简体   繁体   中英

Center UImage with AutoLayout in Xcode

I'm using a Launch Screen.xib file for the launch screen of my iOS app. The app can be used in portrait mode only for iPhone and landscape mode only for iPad.

As a launch screen, I'm using an UImageView. For the portrait mode, I want the image to have a maximum width, and for landscape mode, I want it to have maximum height. It's a square image (512 by 512px), and the ratio has to be preserved.

Does anyone know how to do this in AutoLayout using constraints? I can make it work for one orientation, but it's messed up for the other one.

To have a square image centered in your display, do the following:

  1. Start with a fresh ViewController in your Storyboard. Set the background color to black.

  2. Drag out an Image View and place it in the center of the ViewController. Pin its left, top, right, and bottom edges to its superView. Make sure Constrain to margins is unchecked, and make all four offsets 0 . Click on Add 4 Constraints .

  3. Set the imageView image to your image file, and set the Mode for the View to Aspect Fit .

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