我将一个UIView添加到UIScrollView
并限制它,使其填充水平空间,除了一些利润率。我的视觉约束是这样的:UIView不遵守自动布局约束UIScrollView
@"|-16-[theLineView]-16-|"
我曾认为一个像素高,所以它会显示为一条线,并将其放置两个文本标签之间:
@"V:[someOtherStuff]-[aTextLabel]-[theLineView]-[anotherLabel]"
然而,我发现该线的宽度只扩展到最上面/最下面的标签的宽度。
这是为什么?
P.S我已阅读本http://developer.apple.com/library/ios/#technotes/tn2154/_index.html
代码
这里是由显示在iPad上的SIM这个问题一个测试项目视图控制器代码的全部。
- (void)viewDidLoad
{ [super viewDidLoad];
self.scrollView = [[UIScrollView alloc] init];
self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
self.scrollView.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.scrollView];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[scrollView]|"
options:0
metrics:0
views:@{@"scrollView":self.scrollView}]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|"
options:0
metrics:0
views:@{@"scrollView":self.scrollView}]];
self.line1 = [[UIView alloc] init];
self.line2 = [[UIView alloc] init];
self.label1 = [[UILabel alloc] init];
self.label2 = [[UILabel alloc] init];
self.label3 = [[UILabel alloc] init];
for (UILabel *label in @[self.label1, self.label2, self.label3])
{
label.text = @"I am a label and I am long enough that I can be multiline on an iphone but single on ipad";
}
for (UIView *view in @[self.line1, self.line2, self.label1, self.label2, self.label3])
{
view.translatesAutoresizingMaskIntoConstraints = NO;
view.backgroundColor = [UIColor redColor];
[self.scrollView addSubview:view];
}
//horizontal layout - all views/labels should fill the horizontal space expect for margin
for (UIView *view in @[self.line1, self.line2, self.label1, self.label2, self.label3])
{
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-16-[view]-16-|"
options:0
metrics:0
views:@{@"view":view}];
[self.scrollView addConstraints:constraints];
}
//vertical layout - stack em up
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[lab1]-[line1(==1)]-[lab2]-[line2(==1)]-[lab3]-|"
options:0
metrics:0
views:@{@"lab1":self.label1, @"line1":self.line1, @"lab2":self.label2, @"line2":self.line2, @"lab3":self.label3}]];
}
你的1px UIView对它有没有其他约束? – Yazid
[view]与[theLineView]是否一样?在选项参数中是否有任何对齐选项设置? – rdelmar
选项参数中没有其他限制或任何内容。我会看看我是否可以在测试项目中复制。是的,这些观点是相同的,纠正了片段。 –