我有一个 UIView,我想在主视图上水平居中,然后在主视图上垂直居中,减去大约 14 个像素。
如果我用 IB 设置它,它可以在 Retina 3.5 上运行,但在 Retina 4 上运行时,它当然会偏移大约 40 像素。
我认为最好的解决方案是根据屏幕高度以编程方式建立这些约束?
最佳答案
使用 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]];
关于ios - 在 IB 中使用 Autolayout 如何以编程方式将 UIView 定位在屏幕中央?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21494039/