Truly universal iOS launch screen using Launch Screen.xib template? - ios

So, starting with Xcode 6 we have got a new super-useful feature of Launch Screen template
According to it's description it can help create a launch screen that will work correctly on every device/resolution/orientation and we don't have to make a bunch of different screens for different resolutions with Assets Catalog anymore.
but... I have failed to find any useful tutorial on how to actually use this to make a single launch screen using a single image for all resolutions (only for landscape).
I know how to make a UI element centered on all resolutions using constraints, but i can't figure out how to make my UIImage scale to fill screen height on all resolutions. If i just resize my UIImage to fill current screen size selected in Simulated Metrics - it will fill only that one, but will not work with other screen sizes..
Can somebody give me a clue? how to make UIImage fill entire screen for all resolutions using this Launch Screen template?

If you simply just want to fill your launch screen with an image in every screen size, you can use leading, trailing, top and bottom constraints between your UIImageView and the superview. So:
Step 1
Add your UIImageView to your LaunchScreen.xib and set whatever image it should display.
Step 2
Select your newly added UIImageView and click the "Pin" icon at the bottom right of Interface Builder.
Step 3
Select all 4 edges og set the constants to 0. This makes sure that your UIImageView has 0 points of space to the superview on every edge.
It looks like this:
That said, you should consider if this is the way to go. The idea of having a .xib file as a launch screen is really intended to replace the need of having to show full screen launch images. Instead, consider placing whatever elements your launch image contains in the interface builder instead. Also remember that you're (unfortunately) not able to tie any code together with your launch screen .xib file.

You need to add an UIImageView to view to your xib file and set the constraints to fill the entire screen, e.g. spacing left, right, top and bottom equal to 0. (You already mentioned you can do this, therefore no more instructions regarding this)
Then you inspect your UIImageView, set your desired image you want to display and choose as view mode Aspect Fill
That way your UIImageView takes the image and scales it up, keeping it aspect ratio while filling its entire content.

Related

How to make an image in LaunchScreen.xib fill the background using Xcode

So I'm trying to setup a full screen background with a logo image centered in its middle with an existing LaunchScreen.xib (for a react native project) and I am having trouble with the autoformat.
I see that I can add constraints to my ImageView to maintain aspect ratio but I'm not sure how to make it fill(crop to fit) the view. When I test on device it just stays the same square as the interface builder and doesn't fill the screen. I've been trying to add different constraints to the image but it doesn't seem to affect it.
Also how do I preview it on different devices as shown in the UI. Selecting different devices doesn't seem to change the square canvas view at all.
BTW I don't want to do it as separate assets for every screen size - I'm curious to learn how to build it using the standard xib interface builder using layout rules.
The xib file was shown by a view that inside a UIViewController.
As you set constraints for UIImage in xib file (e.g. in UIView) you should set constraints of UIView inside the UIViewController.
Then now set constraints of root view to the edges, and set Content Mode of imageView to what you want as apple document said:
The scaleAspectFit and scaleAspectFill modes scale the image to fit or fill the space while maintaining the image’s original aspect ratio. The scaleToFill value scales the image without regard to the original aspect ratio, which can cause the image to appear distorted. Other content modes place the image at the appropriate location in the image view’s bounds without scaling it.

Set UIImage to cover the screen for all device screens, xib file

I have a view controller and the UI for the view controller is represented through a xib file instead of main.storyboard. Here is how it looks like. As you can see I have an UIImage view that acts as a background image and is suppose to cover the whole screen.
I am trying to add four constraints: Make all four spacing to the nearest neighbors equal to 0. But when I use the pin constraint option and set pin all sides to 0, it doesn't make the spacing to the nearest neighbor zero but gives me the following four constraints:
So then I tried to control drag from the uiimage to the edge of the frame but I can't do that either. How can I solve this issue?
Also forgot to mention an important thing. When I run the app there are black spaces in the top and bottom of the screen. So the UIImage does not cover the whole screen. Here is a picture while the app is running:
Your problem is launchscreen.
You need to check the launchscreen ,if it is not in correct format or
there is no launchscreen means this kind of problem will occurs .

Swift: How to scale image on LaunchScreen for iPad

Is there a function in the attributes inspector or elsewhere that allows you to scale an image on the LaunchScreen.xib so that it looks good both on iPhone and iPad? I know how to do it with fonts, but images?
You can accomplish this using Asset Catalogues and Auto Layout.
Auto Layout
First, drag an image view onto the canvas (should be a square if "use size classes is checked"). This is where your image will go. To dynamically change the size of the image, we will simply give the image view some rules about how it should lay itself out. Control-drag from the image view to the view behind it. If you do this in the canvas, the visible constraint options will correspond to the general direction you drug the connection line to. If you control-drag between the elements in the project outline, you will get all options. You will need to set enough constraints so that no sizing is ambiguous and no constraint lines are red.
You can inspect and edit the constraints in the Size Inspector.
If at any point and time you have orange constraints and Xcode gives you a warning saying frames will be different at runtime, simply select the image and update frames from the menu popping out of the right-most icon on the bottom-right of the canvas (triangle in-between two lines).
By the way, double-clicking any of these blue constraints lets you edit them.
Asset Catalogues
Now, you just need to add the image to the image view. First, navigate to the Asset Catalogue (.xcassets file) you like to use. Click the plus button at the bottom and add a "New Image Set". In the first box you should drag the image that you would like, whatever size you want (do test). #2x is double the pixel width and height and #3x is likewise triple. These higher resolutions will take up the same amount of physical space but look better with the higher pixel density retina displays.
At this point, your launch screen should look good on any sized device, and on iPad Split Screen if you choose to support it.
Here's an Apple doc on Icons and Graphics.

How do you scale a button on Xcode 6 to fit different screen sizes using Autolayout?

I am new to Xcode and learning about Autolayout. Having a pretty difficult time and think I've finally hit the wall and really need some guidance on the following.
So I created an image view with a .png background image that show button place holders to help me figure out the exact place I would like to place my button objects. So I pinned the image view on all four sides to 0 and it resized perfectly to different screen sizes. Now the problem came with the button. I dragged the button object out of the Object Library and placed it right on top of the image view, where the button place holder image lies. To the button, I gave constraints to its width and height, pinned the bottom and right, and Aspect Ratio to help resize accordingly to different screen sizes.
The first problem is that although the button is placed exactly where the image view shows, the button is located elsewhere, even though the image view is able to perfectly resize to different screen sizes. The second problem is that, although the button pins fine on all screens, the pinned lengths are staying static and not changing even though the screen goes bigger or smaller. The last problem is that the size of the button in itself is staying static as well even on different screen sizes.
Why is the button not able to resize (used Aspect Ratio but not working) and reposition in relative to different screen sizes? A guidance on this would be really appreciated! Been trying to figure this out for a whole day.
NEW UPDATE WITH TAKING OUT WIDTH AND HEIGHT CONSTRAINTS, LEAVING ASPECT RATIO ALONE:
you should remove that width constraint to let the button resize just leading and trailing spaces are fine to increase it's width in different screen sizes

UIButton background image not sizing right with autolayout?

I'm simply trying to fill the entire width of an iPhone screen with a login button for my users that is basically an image set as the background of a UIButton. This is the result I end up with (Notice it fills width as intended, but shrunken down in height):
My thought process was to make 3 images to compensate for #1x #2x #3x resolutions with different image sizes?
#1x: 144x33px
#2x: 288x65
#3x: 432x97
Now from there I thought I could just fill the width of my ViewController with the UIButton in Storyboard and set the background image of the UIButton which I would think would use the proper one automatically depending on device considering I put them in the right spots in Images.xcassets. I also set the trailing and leading constraints as well as bottom constraint to keep it filling left to right and stick to bottom. I just don't get how I would fix the height issue if it's looking fine in the actual XCode storyboard, but not in the preview or simulator as seen in above photo.
Any help would be tremendously appreciated.
The different image sizes are not used to replace each other when you scale an image. They are used to look nice when there are more actual pixels than software pixels.
For your purposes, you probably want to scale the image with the stretching rules or cap insets.
Check out this answer for an example:
https://stackoverflow.com/a/13367861/1864591

Resources