different images depending on user screen size - ios

I'm working on a project where I have a UIButton going from horizontally from edge to edge of the screen. The button in the storyboard is 600 x 242.
I'm set a background image for the UIButton which is also 600 x 242 however my problem is that when running the app on different iPhone screen sizes it just chops off part of the image on the left and right.
Would it be possible to use a different image deepening on the screen size to have everything I need to fit in the UIButton on all screen sizes?

To some extent this can be achieved with size classes. But a much better approach in this case it to set the images in code and choses the image depending on the button size. Or you could try to change the content mode of the button.

Related

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

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

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

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.

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

Is it possible to use IB to automatically change the size of items upon rotation?

I have a view in IB which contains an image. When viewed in portrait mode the image is fairly large and if the device flips to portrait the image need shrinking to fit the narrower screen height.
Is it possible in IB to specify two sizes for image, one for landscape and one for portrait?
You can use IB to automatically adjust image views to be relative to its surrounding views which if you can make work is a good solution but if you need exact pixel sizes you will probably need to do that in code.

Resources