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.
Related
I'm making a splash screen, but problem is, when i change to landscape , it will not fit the screen, so, how can i fix this
Here is on portrait mode:
And when switch to landscape:
So how to fix this? Thank you guy a lots
So, the first thing I do is seperate the background from the logo. As a "general" rule of thumb (for me) I try and keep the logo within about 300x300 (# x1), but you could also make constrain the width and/or height the logo to the view's width/height. Applying a ratio constraint would also allow you to scale the logo as a percentage of the view, while maintaining its aspect ratio (or just use "Aspect Fit" on the UIImageView), but every splash screen seems to want to be slightly different.
I drop a UIImageView onto the launch view and, making sure to turn off "Constrain to Margin" and ensure I'm referencing the UIView and not the "Safe Area", pin it to all the edges
I then assign the image I want to use as the background to UIImageView and select "Aspect Fill" to ensure that the aspect ratio is maintained, but it will fill (or overflow) all the available area of the image view
Next, I add another UIImageView. Here I setup the sizing constraints for the image. In my case, I made a square logo of 300x300px so I don't need to mess about with it and the constrain it to be centred in the launch view
Then I apply the logo image I want to use and ensure the UIImageView is set to "Aspect Fit" (which is now default for Xcode 11+)
Also note that I've offset the UIImageView's vertical position so it's slightly offset from the middle, don't ask me why, but it looks weird been in the middle.
You can then use the storyboard editor to explore the various devices and orientations
You could also explore things like "size class" so you could customise how the layout changes between different device "size classes", but I think that would be overkill for something like this
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.
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.
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
I have created a screen with 2 images on it (150 x 150 pixels) designed to take up most of the screen. This looks fine in the simulator, but when I actually run the app on the device, the images have shrunk down to become very tiny. I assume that the device sizes are the size of the actual image itself.
I used very high quality images so you can't notice that the image has been stretched on the simulator.
Is there something that I can use to ensure that I am always going to use the 150x150 size?
Many thanks
Jon
from what you described, this can be fixed by simply setting the constraints for each object on your storyboard.
To give you an example of how to do this, I've added two UIImageView objects to the storyboard. Select the first object and press the constraints button at the bottom like in the picture below, and you will need to make sure at least 4 constraints have been set. Specifically, check the HEIGHT, WIDTH and select the distance to LHS and distance to TOP by selecting the corresponding struts which will change to a highlighted orange colour (see picture)
Once this has been done, press the 'Add 4 Constraints' button at the bottom. Then repeat this with the second UIImageView object.
When the constraints are properly set (with no conflicts) they will be visible if you select the object, and the constraint lines will be blue (see pic below)
By following these steps for adding objects to your storyboard, then this will solve the issue of the squashed image on the device. Further reading on configuring auto-layout constraints would be very useful.
I hope this helps
Just check your image name, because device is case sensitive but simulator is not.After changing the image name,if it will work then fine.
you can add a 2x version of image(as: yourImage#2x.png).
Remove installed app from device, and run it again.
I think it will help you.