UIButton background image not sizing right with autolayout? - ios

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

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 to make iOS ImageView full match window?

I am new in iOS , I am an android developer self-learing iOS just for fun.
As you can see from the picture , I want to make my ImageView full-displayed in the window in different inch iPhone.
If you are an android developer, you may know what I mean is how to make ImageView match_parent in both width and height?
You must set the UIImageView Constraints, like this to adjust in the full screen, 0 spacing in all sizes, and click in Add 4 Constraints
After that, you must check in the UIImageView settings the mode to Aspect Fit, so it will keep the aspect ratio of your image.
I Took the image from your print, here is the expected final result.
Pin the image view on all four sides to its superview via constraints with a constant of zero. Now configure the image view's content mode so that it doesn't distort the image, but rather keeps its aspect ratio (there are two ways to do this, and I don't know what one you want, so you will have to experiment).

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

different images depending on user screen size

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.

Resize image background iOS

Hi I have developed an app for ios 7 and the 3.5 display , actually i am applying all the auto layout constraints needed for the 4 inch display but for me it´s not clear if once i apply auto layout the background image will resize to fit the 4 inch display or if i need to have a new background image to fit in the 4 inch display.
From memory, I believe it would stretch/shrink the image if you used the same image and allowed to automatically resize. I think that the best course would be to have two images and set the proper image programmatically, deepening on UIDevice's idiom property.
If you don't care if the top and bottom edges get cut off on the smaller screen, you could simply make the UIImageView the size of a 4-inch screen, delete any constraints, and center the view. Good luck!
You should use the image compatible with the 4-inch display and set the ImageView contentMode property to UIViewContentModeScaleAspectFit in the storyboard or in your code. So no matter what the screen size is, it will automatically be adjusted and the image will not even get pixelated.
That depends on .contentMode property. The default value of this property is UIViewContentModeScaleToFill. In this mode your image will be stretched to fill UIImageView's bounds. But you might be interested in UIViewContentModeScaleAspectFit or
UIViewContentModeScaleAspectFill in order to preserve aspect ratio.

Resources