简体   繁体   中英

Xcode Constraints causing image to enlarge to 1920 x 1920

Here's a screenshot of the problem:

How it appears in Xcode:

在此处输入图片说明

It's got content compression resistance priority of h:750 v:750, content hugging priority of h:251 v:251, trailing / leading space of 5, width and height equals to 390, top space of 40, and a 1:1 ratio.

EDIT: I'm using Auto-layout.

There are other elements that rely on it, but I'd rather get it displaying properly first.

Here's how it looks like in the iPhone 6 Simulator:

在此处输入图片说明

You have too many constraints set which conflict with each other. In particular fixing the width and the leading and trailing at the same time will conflict. So will setting the height and the top/bottom conflict. You are probably getting an error in the console telling you it has had to break some constraints.

If you want the image positioned as per your post and to grow with 1:1 to fill either width or height all you should need is:

  • Set the leading constraint to 5.

  • Set the top constraint to 40.

  • Set the trailing to be >= 5.

  • Set the bottom to be >= 5.

  • Set the aspect ration to be 1:1

This is saying that you want the image 40 from the top, and a margin of at least 5 on the right and bottom and an aspect of 1:1. The image view will grow to fit based on the constraint which limits its growth: normally the right in portrait or the bottom in landscape.

You should not set the width or height unless you want to fix or limit it. However if you do that you can not set trailing or bottom.

The image inside will show based on how you have set the image view to present the image: apsect fit, aspect fill etc...

Updated

I just noticed you have the image in a scroll view. I think you need to put the image view in a view which you give a defined size then add that to the scroll view. You may need to do that in code to deal with multiple devices. The scroll view needs to know the content size of the content view. See this tech note: https://developer.apple.com/library/ios/technotes/tn2154/_index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM