以编程方式使用约束来集中UIIMageView

nar*_*ner 4 objective-c xib uiimageview ios autolayout

我正在开发一个iOS项目,我需要以编程方式为我的视图使用约束.我已经习惯了故事板,但由于项目规范的性质,我正在为这个特定项目使用xib.

我有一个MainViewController,在其中我在.h文件中创建以下属性:

@property (nonatomic, strong) IBOutlet UIImageView *backgroundImageView;
@property (nonatomic, strong) IBOutlet UIImageView *logoImage;
Run Code Online (Sandbox Code Playgroud)

我将这些UIImageView实例添加到我的XIB文件中,并通过Attributes检查器选择了适当的图像.

在我的.m文件中,我有一个addConstraints方法,它被调用viewDidLoad.在此方法中,我确保关闭将autoresizingMasks转换为约束:

self.backgroundImageView.translatesAutoresizingMaskIntoConstraints = NO;
self.logoImage.translatesAutoresizingMaskIntoConstraints = NO;
Run Code Online (Sandbox Code Playgroud)

然后,我设置了约束,以便背景图像占据整个superview:

id mainView = @{@"background": self.backgroundImageView};

//Set constraints so that the background takes up the entirety of the superview
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[background]|" options:0 metrics:nil views:mainView]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[background]|" options:0 metrics:nil views:mainView]];
Run Code Online (Sandbox Code Playgroud)

最后,我设置了约束,以便徽标视图位于视图的中心(这是我出错的地方):

// Width constraint
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.logoImage
                                                 attribute:NSLayoutAttributeWidth
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:self
                                                 attribute:NSLayoutAttributeWidth
                                                multiplier:0.5
                                                  constant:0]];

// Height constraint
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.logoImage
                                                 attribute:NSLayoutAttributeHeight
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:self
                                                 attribute:NSLayoutAttributeHeight
                                                multiplier:0.5
                                                  constant:0]];

// Center horizontally
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.logoImage
                                                 attribute:NSLayoutAttributeCenterX
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:self
                                                 attribute:NSLayoutAttributeCenterX
                                                multiplier:1.0
                                                  constant:0.0]];

// Center vertically
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.logoImage
                                                 attribute:NSLayoutAttributeCenterY
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:self
                                                 attribute:NSLayoutAttributeCenterY
                                                multiplier:1.0
                                                  constant:0.0]];
Run Code Online (Sandbox Code Playgroud)

我收到的错误是

***+ [NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]:约束项必须是UIView或子类的实例'

我不完全理解,因为我的约束项(两个UIImageView实例)是其中的子类UIView,但我可能会误解这一点.任何人都可以帮助指出我哪里出错了吗?

Ran*_*ndy 13

试试看:

// Width constraint
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.logoImage
                                                 attribute:NSLayoutAttributeWidth
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:self.backgroundImageView
                                                 attribute:NSLayoutAttributeWidth
                                                multiplier:0.5
                                                  constant:0]];

// Height constraint
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.logoImage
                                                 attribute:NSLayoutAttributeHeight
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:self.backgroundImageView
                                                 attribute:NSLayoutAttributeHeight
                                                multiplier:0.5
                                                  constant:0]];

// Center horizontally
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.logoImage
                                                 attribute:NSLayoutAttributeCenterX
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:self.backgroundImageView
                                                 attribute:NSLayoutAttributeCenterX
                                                multiplier:1.0
                                                  constant:0.0]];

// Center vertically
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.logoImage
                                                 attribute:NSLayoutAttributeCenterY
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:self.backgroundImageView
                                                 attribute:NSLayoutAttributeCenterY
                                                multiplier:1.0
                                                  constant:0.0]];
Run Code Online (Sandbox Code Playgroud)

您试图在您UIImageView和您的视图控制器之间定义约束.您必须在视图之间定义约束,这些视图是同一视图的子视图.