我將一個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
選項參數中沒有其他限制或任何內容。我會看看我是否可以在測試項目中複製。是的,這些觀點是相同的,糾正了片段。 –