[英]How do I center a UIImageView either programmatically or in the storyboard
I have an Image View that I want to center on both the 3.5 and the 4 inch screens. 我有一个要在3.5英寸和4英寸屏幕上居中的图像视图。 For some reason this is proving to be impossible. 由于某种原因,这被证明是不可能的。 I have no idea why I'm struggling with this, it seems so basic. 我不知道为什么要为此苦苦挣扎,似乎很基本。
Is it possible, using constraints or something, to say "center the image on both screens" because it is centered on the iPhone 5, but on the 4, it's aligned with the top, and it cuts off the bottom of the image, because the image is the size of an iPhone 5 screen. 是否有可能使用约束或其他方式说“图像在两个屏幕上居中”,因为它位于iPhone 5的中心,但在iPhone 4上,它与顶部对齐,并且切掉了图像的底部,因为图像是iPhone 5屏幕的大小。
If you want to do it without auto layout and constraints then programatically it would be something like this. 如果要在没有自动布局和约束的情况下执行此操作,则在编程上会像这样。
UIImage *yourImage = ....
UIImageView *imageView = [[UIImageView alloc] initWithImage:yourImage];
[self.view addSubview:imageView];
CGSize superviewSize = imageView.superview.frame.size;
imageView.center = CGPointMake((superviewSize.width / 2), (superviewSize.heigth / 2));
Go to your Storyboard file. 转到您的情节提要文件。 Select your UIImageView and control-drag to it's superview. 选择您的UIImageView并按住Control拖动到它的超级视图。 You should then be presented with a few options. 然后,应该为您提供一些选择。 Click on "Center Horizontally in Container" and then repeat for "Center Vertically in Container. 单击“在容器中水平居中”,然后重复“在容器中垂直居中”。
If you want to have the UIImageView cover the whole superview then click on "Leading Space to Container Margin" and repeat for "Trailing Space to Container Margin", "Top Space to Top Layout Guide" and "Bottom Space to Bottom Layout Guide" instead. 如果要让UIImageView覆盖整个父视图,请单击“将空间引导到容器边距”,然后重复“将空间引导到容器边距”,“从顶部空间到顶部布局指南”和“从底部空间到底部布局指南” 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.