在IB中使用Autolayout如何以编程方式在屏幕中心放置UIView? [英] Using Autolayout in IB how do I position a UIView in the center of the screen programmatically?

查看:94
本文介绍了在IB中使用Autolayout如何以编程方式在屏幕中心放置UIView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UIView that I would like to center horizontally over the main view and then center vertically over the main view, minus about 14 pixels.

如果我设置它与IB一起工作在Retina 3.5上,但是当它在Retina 4上运行时,它当然偏离大约40个像素。

If I set it up with IB it works on the Retina 3.5, but when running on Retina 4 it is of course off by roughly 40 pixels.

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

I am thinking the best solution is establishing these constraints programmatically based on screen height?

推荐答案

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

Use the centerX and centerY constants, and put in a constant of 14 (or -14 not sure which way you want it to be),

[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]];

这篇关于在IB中使用Autolayout如何以编程方式在屏幕中心放置UIView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆