- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我需要垂直 slider 。为此,我有一个包含 UISlider
的 UIView
类,在 initWithFrame:
中,我使用仿射变换将 slider 旋转 90学位。
我现在正在为其添加自动布局支持。
我希望允许 slider 以不同的长度进行实例化,以便它可以在动态的、自动布局的意义上使用。这是我正在尝试的代码,目的是让旋转的 slider 符合 UIView
的边界,位于 initWithFrame:
self = [super initWithFrame:frame];
if(self) {
self.slider = [[UISlider alloc] init];
[self addSubview:self.slider];
self.slider.value = 0;
self.slider.transform = CGAffineTransformMakeRotation(M_PI * 1.5);
[self.slider setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.slider attribute: NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.slider attribute: NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.slider attribute: NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0f constant:0.0f]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.slider attribute: NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f]];
self.backgroundColor = [UIColor colorWithWhite:.8 alpha:1.0];
self.layer.borderWidth = 1;
self.slider.backgroundColor = [UIColor colorWithRed:.21 green:.95 blue:1 alpha:1];
self.slider.layer.borderWidth = 1;
return self;
}
这是结果 View 的样子。我实例化了三个不同的垂直 slider 。左边的帧为 300 x 300,右边的帧为 50 x 300,中间的帧为 300 x 50。我已经为 UIView
框架为灰色,并将 UISliders
的背景着色为蓝色。
300 x 300 的问题是我觉得我负担不起这么大的框架。周围会有其他观点,我担心那个观点会妨碍。
左边的问题是 slider 太小了。
中间那个的问题在于,由于 slider 已旋转出 View 的框架,因此无法捕获拇指并四处移动它。
我也试过将 slider 的顶部限制在 View 的右侧,将 slider 的右侧限制在 View 的底部,但这会产生错误和不可预测的结果。
我需要更改什么才能使这些约束起作用?
最佳答案
比看起来更简单。 UIView 父级全部设置为符合布局约束。它可以更简单地管理其 slider subview 这是整个类。这可以使用 initWithFrame:
在代码中创建,或者将 View 放入 IB 并设置类 == TwistedSlider
。这就是全部...
@interface TwistedSlider ()
@property(weak,nonatomic) UISlider *slider;
@end
@implementation TwistedSlider
- (UISlider *)slider {
if (!_slider) {
UISlider *slider = [[UISlider alloc] init];
[self addSubview:slider];
_slider = slider;
}
return _slider;
}
// this works for any size of this view. the slider will always be as tall as this view is wide
- (void)layoutSubviews {
[super layoutSubviews];
// size it to be as wide as this view's height, center it in this view
self.slider.bounds = CGRectMake(0, 0, self.bounds.size.height, self.slider.bounds.size.height);
self.slider.center = [self convertPoint:self.center fromView:self.superview];
// rotate it
self.slider.transform = CGAffineTransformMakeRotation(M_PI_2);
}
// that's it!
@end
关于ios - 在 subview 上结合仿射变换使用 NSLayoutConstraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34318231/
我正在尝试将 UIView 添加到 UITableView 的标题,然后,使用 NSLayoutConstraints 我想给它一个高度。 我已经浏览了 Apple Docs 和 WWDC 2012
我正在使用 Xcode 9 beta 5 。我务实地添加了反向操作 addConstraint(NSLayoutConstraint.constraints(withVisualFormat: "H:
我使用的是 XCode 版本 9.0 (9A235)、macOS Sierra 版本 10.12.6 (16G29)、iOS 11.0。 我需要将约束添加到数组中,然后我需要激活它。 为此我确实喜欢.
我正在通过代码构建 UICollectionViewCell 界面。一切正常,除了正确的约束。当我运行应用程序时,标题标签的右边距为零。这是我的代码 let titleLabel: UILabel =
我的代码中有两个类,一个是NameCell ,其中包含一个带有文本的简单 UILabel。第二个是NameValueCell ,它继承自该类,但还添加了属性 UIView *valueView . 需
我正在尝试设置一个面板来显示一些信息。我无法让自动布局按我的意愿工作。 这就是我想要做的事情: 我有一个 headerView、一个 UIImageView 和 5 个标签。我希望标签垂直排列,我希望
各位同事, 我开发了一个自定义键盘。键盘类型(字母、数字、特殊字符)之间的切换速度存在问题。这是由于每次重新绘制按钮时的事实。 NSLayoutConstraint我设置如下: 有一个类Keyboar
我试图对 View 内的标签 -> dateLabel 施加约束,这些是我给出的约束: let horizontalConstraint = NSLayoutConstraint(item: date
我使用编程约束将 View 定义为 iOS 屏幕上的弹出窗口。 let stopTimer = StoppageTimer(frame: CGRect.zero) 该 View 本身包含一个堆
我想将 2 个按钮移动到屏幕宽度的中心。它应该看起来像: |SecondButton(100)->| 我从第一个按钮开始。 var const = NSLayoutConstraint(item: f
这是我第一次尝试使用约束,所以请原谅这个初级问题。 我希望能够设置一系列约束,以便 subview 小于 View 宽度的 75% 或 View 宽度的 75%。因此,如果我的 subview 的宽度
我有一个在容器中的 View 在我放置在这个容器中的一些 View Controller 中,我想要全局边距 但我不知道如何将它们添加到容器中。 我的意思是我不知道如何定义 NSLayoutConst
我正在尝试通过 for 循环创建 UILabel,该循环从 NSArray 获取其 NSInteger。循环工作正常,但我看不到所有顶点一个接一个。我肯定是有约束的错误。你能帮帮我吗? 根据我发布的这
我在 UIView 中有 2 个 UILabel: [headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat
我下面的代码使用代码无 Storyboard 来显示对象。该项目已构建,但我看不到我试图以编程方式放置的文本字段。我不知道为什么代码没有出现。我试图不使用 Storyboard。 var enter
我在 Web View 和工具栏之间添加约束时遇到问题。我正在使用以下代码但出现错误。 NSDictionary *viewsDictionary6 = NSDictionaryOfVariableB
我有一个自定义 UITableViewCell,我正在尝试自动布局 UILabel,因为它的文本可以有任意长度,但我收到此错误: Break on objc_exception_throw to ca
我有一个包含 UIWebview 的 UIView。 UIView 具有前导和尾随约束 (20pt)。现在,如果 UIWebview 放大,我通过 UIScrollViewDelegate:scrol
我有一个程序化的 UIToolBar,它是在我的某个 ViewController 的 viewWillAppear: 方法中创建和绘制的。我有 NSLayoutConstraint 设置以将我的 t
我有一个自定义的 UITableViewCell。该单元具有三个标签。最左边的“金额”标签具有以下约束。 在单元格的右侧,我有另一个标签,“Label Dollar Amount”。它具有以下约束:
我是一名优秀的程序员,十分优秀!