我已经以编程方式将图像设置为UIViewController背景。如何以编程方式添加自动布局,以便在iPad和iPhone中显示?
代码如下:
UIImageView *bkImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bkImage.png"]];
[self.view addSubview:bkImage];
答案 0 :(得分:0)
1)使用给出约束的NSLayoutConstraint
类方法之一创建必要的约束。
2)使用UIview's
方法将其添加到视图中以添加约束。
答案 1 :(得分:0)
这增加了0前导,0前导,0顶部和0底部的约束。您可以根据需要修改它们。
[bkImage setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:bkImage
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1.0f
constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:bkImage
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0f
constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:bkImage
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0f
constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:bkImage
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0f
constant:0.0f]];