Center UImage with AutoLayout in Xcode - ios

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:
Start with a fresh ViewController in your Storyboard. Set the background color to black.
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.
Set the imageView image to your image file, and set the Mode for the View to Aspect Fit.

Related

LaunchScreen.storyboard appearing differently on devices than in Xcode?

I am attempting to customize the default LaunchScreen.storyboard of my Flutter app. I simply have a red background on the default View, and I added an Image View as a child of it. This Image View is centered on the screen and renders an image that I have added to "Assets.xcassets" with all 3 sizes. Below is a preview of what my Xcode displays:
However, this storyboard renders differently on devices than it appears in Xcode. For example, it appears slightly off-centered on iPhone 12, and it is scaled up too high for iPhone 6s and is way off-centered. See below for screenshots:
As you can see, the storyboard renders differently on the two devices. How can I make the storyboard appear the same on both devices as it does in Xcode? Any help is appreciated. If I used any incorrect terminology or need to provide additional details, please let me know as I have minimal experience using Xcode.
From the storyboard screenshot attached, it looks like there are no constraints added to the UIImageView.
You should add top, leading, bottom & trailing constraints to superview with 0 spacing to achieve what you want.
UPDATE
Above is applicable to the case when you have a small size image like 100x100 or similar that can easily fit on all screen sizes. In the comments, we see that image is way too big and we want to keep it in center while occupying 50% of screen width.
Remove the leading and trailing constraints, select the imageView and it's superview - add equal widths constraint with a multiplier of 0.5, this will make sure that imageView will always occupy 50% of the superview's width.
Also add horizontally center constraint on the imageView. Make sure imageView Content Mode is set to Aspect Fit.

Fixed Button Position Relative to ImageView in Xcode

In my project I would like to essentially 'Pin' a button to a certain position on an image. The imageView scales to the screen size from the centre and maintains its aspect ratio.
I am trying to make it so that each button remains over the chakras "coloured icons in centre of body". I need them to maintain a scale that is the size of the chakras and maintain their position over the chakras regardless of screen size.
Wanted Result
Current Result
Scaling Settings
Aspect Settings
Here's an example - you will want to play with the exact positioning, but this should show you the method. You can do everything using auto layout in Xcode.
I took one of your images, and edited it to remove the buttons, and used it as the image in a UIImageView filling the screen of a UIViewController
Then I created 7 small buttons, and added constraints to them width & height = 20, and Align Centre Y to Image View
The tricky part is to add a constraint to align the centre of each button to a fixed percentage of the UIImageView centre - and that will be trial and error until it looks right.
Here's how to set the constraint
here's the app running on the iPhone 12 simulator
and here it is on an iPad (12.9 inch)
and here in landscape
You can take parent containerView for each imageView and Button.
Please see the picture to this link for better understand.
https://ibb.co/dsTjxbw

Possible to have different behavior for landscape orientation in launch screen storyboard?

In my Launch Screen.storyboard, I have a UIImageView named LaunchScreenIcon that scales proportionally to the width of the screen by setting the LaunchScreenIcon.width = width constraint.
Is it somehow possible to change the behavior for landscape orientation so that the height of the UIImageView scales to the height of the screen i.e. LaunchScreenIcon.height = height?
Constrain your image view to 0 on all four sides (so it fills the entire view), then set its Content Mode to Aspect Fit
That should give you what you want.
I was able to accomplish this by:
Going to my Launch Screen.storyboard
Selecting the large iPad model in landscape from the panel at the
bottom
Tapping "Vary for Traits" to the right and checking off both width
and height
Then I added the constraint as I normally would and it was now only
associated with the large iPad model in landscape

Launch screen storyboard constraints in iOS Universal app

I'm working on a LaunchScreen.storyboard file for an iOS Universal app in Xcode 7.3.
I must use only constraints defined in storyboard, because the launch screen will be shown before the app will be loaded (can't configure constraints programmatically in a UIViewController subclass).
The background image in the main UIViewController has
aspect-ratio (3456:2592),
center vertically and
center horizontally constraints.
I'm trying to obtain the following conditions:
maintain the background image aspect-ratio,
activate backgroundImageView.Top = containerView.Top when screen width <= screen height
activate backgroundImageView.Leading = containerView.Leading - 20 when screen width > screen height
The problem is that storyboard constraint definition by size classes is not powerful enough to express conditions like screen width >/<= screen height (e.g.: an iPad Air Fullscreen has always regular width and height, but screen width < screen height in portrait mode and screen width > screen height in landscape mode).
I tried also to define the background image asset set by device, but it allows only to specify iPhone/iPad x1, x2 and x3 versions of the image.
You don't need auto-layout for this. Make the UIImageView fill the whole screen, then select the Aspect Fill mode for the background UIImageView.
Result:
Note that the background image (1) keeps the aspect ratio, (2) centered horizontally and vertically, and (3) automatically scale up to fit the longest axis.

UIImageView sizing in AutoLayout + Size classes for all devices

I'm working on app and will for 1st time use auto layout/size classes. Today my first day learning it and I understand constraints now and how to place the images where I want them to be on every devices from iPhone 4s to iPad PRO.
As my main storyboard in Attributes inspector i'm using INFERRED size. In show Assistant Editor, Preview, i'm using all device sizes.
In my inferred storyboard i'm using a UIImageView size 170 Width, 230 Height. Then I use AutoLayout to make it center of every device screen in preview.
Now the issue is this UIImageView covers HALF of the size of iPhone 4s preview but only about 1/5th the size of iPad PRO. How do I alter the size of each UIImageView to the size that I want them to be in each device without changing that centered position of each UIImageView in the preview devices?
You need to first set up your constraints properly and then choose the type of scaling you want.
Set up your constraints:
Click on the Pin icon |-[]-| at the bottom of the screen. Turn on all four orange I-beams. Set the four constants to zero.
Uncheck the Constrain to margins checkbox and click 'add 4 constraints'
Then set your scaling:
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 uncrossed).
That should work
Maybe you can use a pending or proportion constraints instead of width and height constraints.

Resources