中心在iPhone 5和iPhone 4中的界面构建器中对齐

AVE*_*imi 3 iphone user-interface xcode ios

在XCode界面构建器中:我的视图中有一些图像需要它们垂直居中对齐.但Retina 4和Retina 3.5的屏幕高度不同,我不喜欢使用OS6作为相对坐标.无论如何这样做是接口构建器还是我应该编写一些代码?

jrt*_*ton 6

Using Autolayout in interface builder, select your view, choose the constraints menu in the bottom right of the canvas, and choose center vertically in container. This will apply to both screen sizes.


use*_*tbd 5

您可能应该使用实际代码.

幸运的是,它非常简单:只需获得屏幕高度,减去图像高度,然后除以2.

CGFloat height = [[UIScreen mainScreen] bounds].size.height;
CGRect frame = [imageView frame];
frame.origin.y = (height-frame.size.height)/2;
[imageView setFrame:frame];
Run Code Online (Sandbox Code Playgroud)

  • 这是正确的(代码可以工作)和错误的(你不需要它,并且编程创建一个居中约束比框架算术更容易). (2认同)