iOS自动布局视图未显示在屏幕上?
当我使用initWithFrame时,一切都很好。但我的主管建议,自动布局通常是一种更好的做法。所以我试着给imageView添加4个约束,当我在模拟器上运行它时,它突然不会显示在我的屏幕上iOS自动布局视图未显示在屏幕上?,ios,objective-c,autolayout,Ios,Objective C,Autolayout,当我使用initWithFrame时,一切都很好。但我的主管建议,自动布局通常是一种更好的做法。所以我试着给imageView添加4个约束,当我在模拟器上运行它时,它突然不会显示在我的屏幕上 - (UIImageView *) titleImage { if (!_titleImage){ _titleImage =[[UIImageView alloc] init]; _titleImage.image = [UIImage imageNamed:@"E
- (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];
当我刚刚使用initWithFramex
,y
,宽度
,高度
时,它工作正常。我删除了这一行并添加了4个约束,现在我无法让它显示在我的屏幕上
[_titleImage setTranslatesAutoresizingMaskIntoConstraints:NO];
[_titleImage setTranslatesAutoresizingMaskIntoConstraints:NO];
您需要在添加子视图后添加约束,这样我就不能在setter中添加约束?您需要在添加子视图后添加约束,这样我就不能在setter中添加约束?