[英]iOS autolayout with constraints. Some of TableViewCell is showing off screen
[英]iOS autoLayout views not showing on screen?
當我使用initWithFrame時,一切都很好。 但是我的主管建議,autoLayout通常是更好的做法。 因此,我嘗試在imageView中添加4個約束,但是當我在模擬器上運行它時,它突然不會顯示在屏幕上。
- (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個約束,現在我無法在屏幕上顯示它。
_titleImage
添加為子視圖。 在與AutoLayout一起使用的任何視圖上,都必須調用:
[_titleImage setTranslatesAutoresizingMaskIntoConstraints:NO];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.