So I have an image view and a button. I need the image view to be the correct size (not look squished or stretched) on all devices and more importantly I need it to be the size of the screen. I need the button to stay in about the same place on all devices.
Back with Xcode 5 I remember auto layout fixing this problem but now that does not work. I looked at https://www.youtube.com/watch?v=G53PuA_TlXk and tried control-dragging to the view controller and centering the view both horizontally and vertically, but I can't get it to scale to the size of the screen. The video doesn't address this.
Is there a specific option I need to choose when control-dragging to keep the image view scaled? What should I do?
Click on the Image View you added to your View Controller. In the Attributes Inspector on the right, choose your image. Set the Mode of the View to Aspect Fill (fill entire screen cropping top/bottom or left/right as necessary) or Aspect Fit (letter box image so that entire image is shown uncropped).
Auto layout is a property you can add to frames and components. It lets you create designs that grow to fill or shrink to fit, and reflow as their contents change. This is great when you need to add new layers, accommodate longer text strings, or maintain alignment as your designs evolve.
You need to do 2 things:
Setting Up Your Constraints:
Choosing Your Scaling:
You can view the constraints that have been created for you. In the document outline, you should see 4 constraints under your image view:
Click on a constraint and then view them in the Attributes Inspector on the right. Here are the 4 that I created:
This is what worked for me:
View Mode:
Aspect Fill
As constraints for the image:
Align Center X to: Superview
Equal width to: Superview
The 'Equal width' property is the one that scale the image to the size of the screen.
If you want to have the button in a specific position, set constraints from it to the view, the main ones you want to consider are a top constraint and any left or right constraints. So you are telling the view where you want to position the button in relation to its sides.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With