在IB中使用Autolayout如何以编程方式在屏幕中心放置UIView?

jdo*_*dog 1 objective-c ios autolayout

我有一个UIView,我想在主视图上水平居中,然后在主视图上垂直居中,减去大约14个像素.

如果我用IB设置它可以在Retina 3.5上运行,但是当在Retina 4上运行时它当然偏离大约40个像素.

我认为最好的解决方案是根据屏幕高度以编程方式建立这些约束?

rde*_*mar 9

使用centerX和centerY常量,并输入一个14的常量(或-14不确定您希望它是哪种方式),

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeCenterX relatedBy:0 toItem:view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:view attribute:NSLayoutAttributeCenterY multiplier:1 constant:14]];
Run Code Online (Sandbox Code Playgroud)