Tôi đang thêm UIView vào một UIScrollView
và hạn chế nó sao cho nó lấp đầy không gian ngang, ngoại trừ một số lề. hạn chế hình ảnh của tôi trông như thế này:UIView không tuân theo các ràng buộc tự động hoàn thành trong UIScrollView
@"|-16-[theLineView]-16-|"
Tôi đã thực hiện quan điểm một pixel cao vì vậy nó sẽ xuất hiện như một dòng, và đặt nó giữa hai nhãn văn bản:
@"V:[someOtherStuff]-[aTextLabel]-[theLineView]-[anotherLabel]"
Tuy nhiên, tôi đang tìm kiếm rằng chiều rộng của đường chỉ mở rộng theo chiều rộng của nhãn dài nhất trên/dưới nó.
Tại sao điều này là?
Tái bút: Tôi đã đọc http://developer.apple.com/library/ios/#technotes/tn2154/_index.html
Mã
Dưới đây là toàn bộ mã xem điều khiển từ một dự án thử nghiệm mà thể hiện vấn đề này trên sim iPad.
- (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}]];
}
Liệu 1px 'UIView' của bạn có bất kỳ ràng buộc nào khác không? – Yazid
Có phải [xem] cùng chế độ xem với [theLineView] không? Bạn có thiết lập tùy chọn căn chỉnh nào trong tham số tùy chọn không? – rdelmar
Không có ràng buộc nào khác hoặc bất cứ điều gì trong tham số tùy chọn. Tôi sẽ xem liệu tôi có thể tái sản xuất trong một dự án thử nghiệm hay không. Có những lượt xem đó giống nhau, đã chỉnh sửa đoạn mã. –