xcode iPhone 5 和 iPhone 4 的界面构建器中的居中对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13599287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:12:08  来源:igfitidea点击:

Center Align in interface builder in both iPhone 5 and iPhone 4

iphoneiosxcodeuser-interface

提问by AVEbrahimi

In XCode interface builder : I have some images in my view that I need them to be center aligned vertically. But screen height of Retina 4 and Retina 3.5 differs and I don't like to use OS6 for relative coordinates. Is there anyway to do this is interface builder or I should write some code?

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

回答by jrturton

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.

在界面构建器中使用自动布局,选择您的视图,选择画布右下角的约束菜单,然后在容器中垂直选择居中。这将适用于两种屏幕尺寸。

回答by username tbd

You should probably use actual code for this.

您可能应该为此使用实际代码。

Luckily, it's fairly straightforward: just obtain the screen height, subtract the image height, and divide by two.

幸运的是,它相当简单:只需获取屏幕高度,减去图像高度,然后除以 2。

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

回答by Rahul Wakade

You can use autoresizing(IB->Size inspector) for it. But you will not see autoresizing options if auto layout is set for xib. You have to remove auto layout(IB->File inspector) first & then set autoresizing properties as you want.

您可以使用 autoresizing(IB->Size inspector) 为它。但是如果为 xib 设置了自动布局,您将不会看到自动调整选项。您必须先删除自动布局(IB->文件检查器),然后根据需要设置自动调整大小属性。