Auto Layout keeps stretching UIImageView

爷,独闯天下 提交于 2019-12-18 18:53:28

问题


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.

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....

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.


回答1:


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).

In interface builder, make sure the Clip Subviews box is checked.

If you're not using interface builder, the following code will do the same thing:

yourImageView.clipsToBounds = YES;



回答2:


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.

  • 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

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)




回答3:


My problem was with the ContentHuggingPriority and 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:

[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!




回答4:


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.




回答5:


You can try to add UIImageView programmatically. Call this code in your viewDidLoad method or where you want.

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];


来源:https://stackoverflow.com/questions/20844128/auto-layout-keeps-stretching-uiimageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!