Xcode 6: How to make an image view fill the screen on all devices? Auto layout not working?

后端 未结 4 1547
孤独总比滥情好
孤独总比滥情好 2020-12-22 22:27

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 s

4条回答
  •  再見小時候
    2020-12-22 23:17

    You need to do 2 things:

    1. Set up your constraints properly
    2. Choose the type of scaling you want.

    Setting Up Your Constraints:

    1. Drag out an Image View and place it roughly in the center of your view.
    2. Set your size class to wAny hAny.
    3. Click on the Pin icon |-[]-| at the bottom of the screen.
    4. Turn on all four orange I-beams. Set the four constants to zero.
    5. Uncheck the Constrain to margins checkbox.
    6. Click Add 4 constraints

    Add New Constraints

    Choosing Your Scaling:

    1. Click on the Image View you added to your View Controller.
    2. In the Attributes Inspector on the right, choose your image.
    3. 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).

    Attributes Inspector


    You can view the constraints that have been created for you. In the document outline, you should see 4 constraints under your image view:

    Document Outline

    Click on a constraint and then view them in the Attributes Inspector on the right. Here are the 4 that I created:

    Leading edge constraint

    Trailing edge constraint

    Top edge constraint

    Bottom edge constraint

提交回复
热议问题