Когда я использовал initWithFrame, все было в порядке. Но мои руководители сообщили, что autoLayout - это, как правило, более эффективная практика. Поэтому я попытался добавить 4 привязки к imageView, и внезапно он не появится на моих экранах, когда я запустил его на симуляторе.iOS autoLayout просмотр не отображается на экране?
- (UIImageView *) titleImage
{
if (!_titleImage){
_titleImage =[[UIImageView alloc] init];
_titleImage.image = [UIImage imageNamed:@"Example.png"];
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:_titleImage
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:100]];
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:_titleImage
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:100]];
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:_titleImage
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:-100]];
[self.view addConstraint:
[NSLayoutConstraint constraintWithItem:_titleImage
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:-100]];
}
return _titleImage;
}
А потом в методе loadView у меня есть ...
[self.view addSubview:self.titleImage];
Когда я использовал initWithFrame x
, y
, width
, height
он работал отлично. Я удалил эту строку и добавил 4 ограничения, и теперь я не могу заставить ее отображаться на моем экране.
Вы должны добавить ограничения ПОСЛЕ addSubview –
поэтому я не могу добавить ограничения в моем инкубаторе? – BeefJurky