How to make a square view resize with its superview using auto layout
In my particular case I have view inside the view controller for which I added the following constraints:
- Set leading, trailing, top and bottom edges to 0
- Set multiplier to bottom edge to 2:1
Now the view sits in the top half on the view controller.
Inside this I add a square image view, for which I added the following constraints:
- Ctrl drag from image view to superview and added equal heights and widths.
- Change the multiplier for width and height until I have a perfect square.
- Added constraints to be center vertically and horizontally
My constraints seems perfect, but when running in the simulator I don’t get a perfect square. Besides this, the image view doesn’t get resize when running on different simulator screens.
This is my setup:
- Auto layout and size classes are enabled
- I use an Inferred size for the storyboard
- Adaptive layout is set to Any for width and height
- I am trying to run this for the 4s,5,6 and 6+ simulators.
I looked on other stackoverflow posts, but nothing seems to work.
Are there some basic steps to do this ?
After setting >=10 constraints:
edit 3: I added top,bottom,leading,trailing constraints 2 times, 1 with lower than or equal(priority 1000), the other one with greater than or equal(priority 800) with the constant value of 90. I don’t know why for bottom it tries to streches to 90 pt from the main view, not the container view(green one).
- Centering a UIView programmatically using Autolayout makes the view disappear from superview
- iOS - Custom table cell not full width of UITableView
- UIScrollView zooming with Auto Layout
- iOS: Autolayout causing UIScrollView to not scroll
- How to update the constant height constraint of a UIView programatically?
- UIImageView .scaleAspectFit and autolayout not working programmatically from Swift
3 Solutions Collect From Internet About “How to make a square view resize with its superview using auto layout”
You have a view that needs to expand to fill its container while maintaining its aspect ratio. This is a common pattern with Auto Layout.
The trick is to use two constraints for leading/trailing/top/bottom:
=10at low priority
>=10at required priority.
Putting it all together, you have:
Aspect Ratio 1:1
Center X/Y in Superview
Leading/Trailing/Top/Bottom to Superview = 10 (at 750 priority)
Leading/Trailing/Top/Bottom to Superview >= 10 (at 1000 priority)
There are also a couple of things to consider with UIImageView:
UIImageView will have an intrinsic content size based on the image that it is displaying, so you’ll want to ensure that its Content Hugging Priority is lower than the 750 priority you use for the
UIImageView.contentModedetermines how the underlying image is sized relative to the size of the UIImageView. By default, it’s set to
container view ---------------------------------------------- | | | | >=10 | | imageView | | | ---------------------------- | | | | | | | | | | | | | | | | | | | | | | | | | | |- >=10 -|---------- 1:1 -----------|- >=10 -| | | | | | | | | | | | | | | | | | | | | | ---------------------------- | | | | | >=10 | | | | ----------------------------------------------
**If you want you can specify the imageView’s height or width as well with a lower priority constraint.
I had success using this configuration.
What I did was first add constraints for center X. (Ignore the center Y one even though it’s in my screenshot. It will break regardless because of step 2.)
Then I added a top and bottom constraint
Finally I added a aspect ratio constraint
When I animated this, the box scaled as a square correctly. If you want I can upload the Test project.
- How do I change the height of a UITableView to fit its dynamic content?
- background: fixed no repeat not working on mobile
- Security error in ios 9 in ccAvenue payment gateway
- Expand UITableView Header View to Bounce Area When Pulling Down
- Swift – Create data model from JSON response
- .on(“click”) does not work on iOS
- Force reload watchOS 2 Complications
- XCode debug vs release build when debugging
- Why is the extension of my Swift class not visible outside the defining file?
- How to create custom signal in ReactiveCocoa 4?
- MPMoviePlayerController Audio show “Done” Button
- How to fix Failed to fetch default token error?
- Showing Accurate Progress In UIProgressView While Downloading Images in iphone
- How to adjust an UIButton's imageSize?
- Scheduling local notification from within a Today extension