ios 自动布局不断拉伸 UIImageView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20844128/
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
Auto Layout keeps stretching UIImageView
提问by Gyuhyeon Lee
I am using Auto layout and it's driving me crazy. I have done everything I could to prevent UIImageView from stretching all over. I have no idea why it does that. I tried using constraints, solving auto layout issues. For now, the only solution was turning the auto layout itself.
我正在使用自动布局,它让我发疯。我已经尽我所能来防止 UIImageView 拉伸。我不知道为什么会这样。我尝试使用约束,解决自动布局问题。目前,唯一的解决方案是改变自动布局本身。
Could anyone please tell me why xcode is completely ignoring my constraints? I would expect the UIImage to stay as 320x320 when I explicitly set it as that, but noooooooo....
谁能告诉我为什么 xcode 完全无视我的约束?当我明确地将其设置为 320x320 时,我希望 UIImage 保持为 320x320,但是 noooooooo ....
I want to post images, but the site won't let me, and I would like to add code to this question so that it is more specific, but there's literally no code at all. I just dragged "UIImageView" from storyboard, made it small, set constraints as is, and it's just ignoring my code.
我想发布图片,但网站不让我发布,我想在这个问题中添加代码,以便更具体,但实际上根本没有代码。我只是从情节提要中拖出“UIImageView”,将其缩小,按原样设置约束,而它只是忽略了我的代码。
回答by Travis
You'll want to make sure your UIImageView
is set to clip to bounds. If not, the image will spill out of the view and will appear to not stretch correctly (even though in reality it is).
您需要确保您UIImageView
设置为裁剪到边界。否则,图像将从视图中溢出并且看起来不能正确拉伸(即使实际上是这样)。
In interface builder, make sure the Clip Subviews box is checked.
在界面构建器中,确保选中 Clip Subviews 框。
If you're not using interface builder, the following code will do the same thing:
如果您不使用界面构建器,以下代码将执行相同的操作:
yourImageView.clipsToBounds = YES;
回答by GeneralMike
With autolayout, the values in the Size Inspector are pretty much ignored.
使用自动布局,尺寸检查器中的值几乎被忽略。
You might expect that if a number is typed into the boxes on the Size Inspector, Xcode would automatically create constraints to reflect what you typed, but it doesn't. You need to create "Pin" constraints on your own for the height and width of your UIImageView
.
您可能希望如果在 Size Inspector 上的框中输入一个数字,Xcode 会自动创建约束以反映您输入的内容,但事实并非如此。您需要自己为UIImageView
.
- Select the image view on your storyboard
- Click the "Pin" button at the bottom-right of the storyboard screen
- Type the desired size into the "Width" and "Height" fields, and make sure the boxes are checked
Click the "Add 2 constraints" button
- 选择故事板上的图像视图
- 单击故事板屏幕右下角的“固定”按钮
- 在“宽度”和“高度”字段中输入所需的尺寸,并确保选中这些框
单击“添加 2 个约束”按钮
When you are done, you should be able to see your constraints in the Size Inspector on the right side of the screen (the purple boxes)
完成后,您应该能够在屏幕右侧的 Size Inspector 中看到您的约束(紫色框)
回答by Noam Nelke
My problem was with the ContentHuggingPriority
and ContentCompressionResistancePriority
.
我的问题是ContentHuggingPriority
和ContentCompressionResistancePriority
。
The first controls how important it is to keep the view "hugging" the image (staying close to it and not expanding) and the latter controls how resistant to compression the view is.
第一个控制保持视图“拥抱”图像(靠近它而不扩展)的重要性,后者控制视图对压缩的抵抗力。
We use PureLayout, so the code to fix it was:
我们使用PureLayout,因此修复它的代码是:
[NSLayoutConstraint autoSetPriority:ALLayoutPriorityRequired forConstraints:^{
[myImageView autoSetContentCompressionResistancePriorityForAxis:ALAxisHorizontal];
[myImageView autoSetContentHuggingPriorityForAxis:ALAxisHorizontal];
}];
(my problem was only in the horizontal axis)
(我的问题仅在水平轴上)
May 15, 2016:Updated to PureLayout v3 syntax. If you use a version <= 2 just put UIView
instead of NSLayoutConstraint
. Thanks Rudolf J!
2016 年 5 月 15 日:更新到 PureLayout v3 语法。如果您使用的版本<= 2只是把UIView
代替NSLayoutConstraint
。谢谢鲁道夫J!
回答by JanB
I had the same problem. I added a UIImageView into a prototype cell and wanted to align it next to the text but it kept stretching the image when I applied the constraints. In my case, I changed the 'Mode' property for the Image View to 'Aspect Fit' and it worked fine.
我有同样的问题。我在原型单元格中添加了一个 UIImageView 并希望将它与文本对齐,但是当我应用约束时它一直在拉伸图像。就我而言,我将 Image View 的“Mode”属性更改为“Aspect Fit”,效果很好。
回答by Ilya Dmitriev
You can try to add UIImageView programmatically.
Call this code in your viewDidLoad
method or where you want.
您可以尝试以编程方式添加 UIImageView。在您的viewDidLoad
方法中或您想要的地方调用此代码。
UIImageView *yourImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50.0f, 50.0f, 320.0f, 320.0f)]; // x and y coordinates, width and height of UIImageView
[yourImageView setImage:[UIImage imageNamed:@"your_image.png"]];
[yourImageView setContentMode:UIViewContentModeCenter]; // you can try use another UIViewContentMode
[self.view addSubview:yourImageView];