NSLayoutConstraint limitedWithItem неправильно работает с вложенными представлениями в iOS8 Beta 4

Я обновляю свое приложение для iOS8 и заметил изменение поведения с помощью [NSLayoutConstraint limitedWithItem: ..].

Это отлично работает в iOS7, но в iOS8 ограничение с представлением, которое является вложенным подпредставлением, работает неправильно.

NSLayoutConstraint *myConstraint = [NSLayoutConstraint constraintWithItem:myView.mySubview.label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:label2 attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10.0];

По сути, это выглядит так, поскольку координаты myView.mySubview.label y равны 0 в его непосредственном супервизоре (mySubview), он помещает label2 относительно y=0. Однако самый верхний родительский вид (myView) имеет произвольную координату y, которую ограничение не соблюдает. Это как если бы Autolayout просто берет координату y вида в своем непосредственном супервиде, а не в контексте всего. Опять же, эта же строка отлично работает в iOS7.

Если я применю ограничение только к представлениям верхнего уровня:

NSLayoutConstraint *myConstraint = [NSLayoutConstraint constraintWithItem:label1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:label2 attribute:NSLayoutAttributeBottommultiplier:1.0 constant:10.0];

Это отлично работает на iOS7 и 8.

Новое поведение или ошибка iOS8?


person paranoidroid    schedule 26.07.2014    source источник
comment
Вы сообщили об этом? Какой у него номер радара? Что сказала Apple?   -  person Paul Cezanne    schedule 22.08.2014
comment
Похоже, это исправлено с iOS8 GM.   -  person paranoidroid    schedule 10.09.2014


Ответы (1)


Похоже на ошибку iOS8... это также влияет на мой проект. Воспроизводится со следующим фрагментом кода, где я вижу notWorkingLabel в iOS7, но не вижу iOS8:

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIView *container = [UIView new];
    container.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:container];

    UILabel *workingLabel = [UILabel new];
    workingLabel.backgroundColor = [UIColor lightGrayColor];
    workingLabel.translatesAutoresizingMaskIntoConstraints = NO;
    workingLabel.text = @"This is a visible label";
    workingLabel.font = [UIFont systemFontOfSize:20];
    [container addSubview:workingLabel];

    UIView *subContainer = [UILabel new];
    subContainer.backgroundColor = [UIColor yellowColor];
    subContainer.translatesAutoresizingMaskIntoConstraints = NO;
    [container addSubview:subContainer];

    UILabel *notWorkingLabel = [UILabel new];
    notWorkingLabel.backgroundColor = [UIColor lightGrayColor];
    notWorkingLabel.translatesAutoresizingMaskIntoConstraints = NO;
    notWorkingLabel.text = @"This label is not visible";
    notWorkingLabel.font = [UIFont systemFontOfSize:20];
    [subContainer addSubview:notWorkingLabel];

    NSDictionary *views = NSDictionaryOfVariableBindings(container, workingLabel, subContainer, notWorkingLabel);
    [subContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[notWorkingLabel]-|" options:0 metrics:nil views:views]];
    [subContainer addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[notWorkingLabel]-|" options:0 metrics:nil views:views]];
    [container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[workingLabel]|" options:0 metrics:nil views:views]];
    [container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[subContainer]|" options:0 metrics:nil views:views]];
    [container addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[workingLabel]-20-[subContainer]" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[container]|" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[container]|" options:0 metrics:nil views:views]];
}

@end
person mrtall    schedule 01.08.2014
comment
Спасибо за проверку. Давайте оба представить ошибки на этом. - person paranoidroid; 02.08.2014
comment
Вы сообщили об этом? Какой у него номер радара? Что сказала Apple? - person Paul Cezanne; 22.08.2014
comment
UIView *subContainer = [новый UILabel]; //неверно, используйте - person Paul Cezanne; 02.09.2014
comment
UIView *subContainer = [новый UIView]; // и у тебя все будет хорошо - person Paul Cezanne; 02.09.2014