简体   繁体   English

如何以编程方式或在情节提要中将UIImageView居中

[英]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.

相关问题 如何在UITableViewCell中将UIImageView水平居中? - How do I horizontally center a UIImageView in a UITableViewCell? 如何以编程方式点击UIImageView在iPhone上做点什么? - How do I programmatically make clicking on a UIImageView do something on the iPhone? 如何在全屏 UIScrollView 中居中 UIImageView? - How do I center a UIImageView within a full-screen UIScrollView? 如何从以编程方式创建的UIImageView中选择 - How do I segue from programmatically created UIImageView 如何以编程方式将UIImage添加到UIImageView然后将该UIImageView添加到另一个UIView? - How do I programmatically add a UIImage to a UIImageView then add that UIImageView to another UIView? 如何制作UIImageView,以编程方式在容器中垂直居中 - How to make a UIImageView, center vertically in container with constraints programmatically 我们如何以编程方式获取UIImageView的坐标? - How do we get the coordinates of an UIImageView programmatically? 无论方向如何,如何将UIImageView居中于UITableViewCell中? - How do you center a UIImageView in UITableViewCell regardless of orientation? 如何让 UIImageView 以编程方式更改其图像? - How can I make UIImageView change its image programmatically? 如何以编程方式为黑/白UIImageView着色? - How can I colorize a black/white UIImageView programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM