ios - 在 IB 中使用 Autolayout 如何以编程方式将 UIView 定位在屏幕中央?

标签 ios objective-c autolayout

我有一个 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/

相关文章:

ios - 从第二个 View Controller 到第一个 View Controller ios8

ios - 如何使用 photo_reference 显示来自 google json 响应的图像?

ios - 使用 AutoLayout 更改 UILabel 字体大小

ios - 日期格式为空

iOS 和 RestKit : How to get a text/html response right?

objective-c - UILabel 使用剪辑渲染部分字符

ios - 崩溃报告与 iOS 中的自动布局管理不善有关

ios - 具有自由格式 UIViewController 和 AutoLayout 的 UIScrollView

ios - UIView 隐藏/显示动画

ios - 指向作为参数传入的协议(protocol)的指针