0
當我使用initWithFrame時,一切都很好。但我的主管建議autoLayout通常是一種更好的做法。所以我試圖給imageView添加4個約束,當我在模擬器上運行它時,它突然不會顯示在我的屏幕上。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