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.
Related
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
I am making an app and I have a UIScroll view in my viewController. I have set the simulated size to freeform and the height to 2000 and width to 375. When I run my app on the Iphone 7 There are no problems. But when I try to run the app on the iphone 7plus the screen does not adapt and i get these white bars on the right side because my contents width is only 375 pixels. So how can I make my screen to automatically go bigger when I use a bigger device?
If not already done, you should add contraints (trailing, leading, top space, bottom space) to your UIScrollView to pin it to the superview bounds.
I have a UIImageView in a Storyboard. The image used is square. The imageView width and height are equivalent. I want to take advantage of Autoresizing in Storyboard, so that on a small device the image is scaled down and on a large device the image is scaled up. When I test on a iPhone 5 the scaling is appropriate. However, when running on a iPad the results are different. The square is stretched, and this makes sense as the dimensions of the screen is different to the dimensions of the iPhone. How can I avoid this issue, is there a process in which I can scale the view as a whole up until the height is the same as the height of an iPad and then centre the view?
Note - The image has the same width and height and the content mode is set to 'Aspect Fit'
Follow this procedure
1) add a view in which all your content will be added so now you going to use 2 views NewView[with Content] and baseView
now time to add constraints to newView Do like this for leading, trailing,bottom and top [adding wr hr component]
Result For iPad
result for iPhone
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.
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.