Я добавляю UIView к UIScrollView
и ограничиваю его так, чтобы он заполнил горизонтальное пространство, за исключением некоторых полей. Мое визуальное ограничение выглядит так:
@"|-16-[theLineView]-16-|"
Я сделал вид высотой в один пиксель, чтобы он отображался в виде линии, и поместил его между двумя текстовыми метками:
@"V:[someOtherStuff]-[aTextLabel]-[theLineView]-[anotherLabel]"
Однако я обнаружил, что ширина линии увеличивается только до ширины самой длинной метки над/под ней.
С чего бы это?
PS Я прочитал это http://developer.apple.com/library/ios/#technotes/tn2154/_index.html
Код
Вот полный код контроллера представления из тестового проекта, который демонстрирует эту проблему на симуляторе iPad.
- (void)viewDidLoad
{ [супер 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}]];
}
UIView
какие-либо другие ограничения? - person Yazid   schedule 21.07.2013