issue balancing constraints in Interface Builder when using ratio - ios

I am having a problem with my constraints for an alert view I am building in interface builder.
When the screen is an iPhone 5 size I want the leading and trailing constraints to compress the images and make is smaller, I have a 1:1 ratio constraint set here too. This works great so far. However when the screen gets bigger e.g. iPhone7+ or iPad I want the image to remain the same size (that is the size of the image in interface builder, the iPhone7 size canvas). I have tried setting a constraint on the width of the image using <= and then setting the leading and trailing constraints on the image to priority 750. This works for the bigger screens but now when I go back to the iPhone5 screen size it is broken because the priority on the leading and trailing constraints is less than that of the ratio. The image size remains unchanged. Seems no matter how I work the constraints I can only get the size working for a single scenario iPhone5 or iPhone6+. Has anyone had this kind of issue before? Any suggestions on how I can work the constraints??

Try exploring on size classes. Explore on this link. https://www.raywenderlich.com/162311/adaptive-layout-tutorial-ios-11-getting-started.
Basically by using this you can set separate constraints to a same element with respect to size classes(which in term refers to different sizes of device screens).

You can implement it this way. It works well for iPhones of all sizes.
There are 3 key points regarding the UIView containing the image
Set leading and trailing constraint as greater than or equal (>=)
Center the view horizontally in the superview. This constraint combined with the previous ones will result in the view staying centered even when the image won't be able to stretch anymore.
Set content hugging priority to required (1000).

Related

Image Resizing for different screens XCode

I've been trying to find a solution to this problem for a long time but I haven't been able to solve it. How do I make an image appear smaller so it would fit the screen on a smaller device? When I have in on a larger device it fits fine but when switched to a smaller screen the images start to overlap.
Example of images overlapping
I would really appreciate the help. Thank You!
Use auto layout and set your Uiimageview aspect ratio to 1:1
There are a lot of ways. Some are mentioned below:
Using Auto layout
connect every image to side of main view and set their widths and height ratio 1:1 , also set their width proportional to main view widht.
using stackviews and autolayout
1st set images width proportional to main view width
then set their aspect ratio 1 : 1
then add the columns to 2 vertical stack views and add both columns to horizontal stackview.
set the left right and bottom constraint of final stackview
You can use auto resizing as well, but that is not totally reliable.
autoresizing is default behavior of view. In your case you have to set flexible width and height of each image

Laying Out Login Screen In Xcode - iOS

I have a login layout and I want it to work universally on 4 inch, 4.7 and 5.5 inch screens.
I want everything to be as if the screen was "one" and I just made it all x% larger or bigger to stretch and fit the screen.
Part of the challenge in doing this is that different components are adjusted in different ways to scale with screen size, as well as that there are a few ways to do some of those.
Lets look at a few:
You can use Autoresizing to scale a view automatically with with the size of it's superview/container (so this applies to changing from portrait to landscape). You can find this in the size inspector (if you haven't set constraints). You can see an in depth tutorial on it here.
This let's you set both spacing and internal space based on where you place the view originally.
Another way is setting constraints in relationship to another view with a ratio other than 1:1. For example, you can set a constraint of your button to the superview as equal widths and edit that constraint's ratio/multiplier to 1:2 to make it 50% the size or 1:4 to make it 25% of the width, etc'.
For the fonts you should probably use Autoshrink. You can see a detailed answer about this here.

Auto Layout Setup

I've got the below layout and I've got it working well on iPhones, landscape only.
The PickerViews are currently populated with an image array. Currently, I force resize the images in the PickerViews based upon device size by setting the PickerView row height and image height to what fits each specific device (iPhone 5 -7+). However, to work on an ipad, I need to expand the ImageView in the center.
When I change the hugging values to >= it allows it to grow too large and pushes the PickerViews out the side on iPhones, despite them having a 4nit margin to superview. I've tried playing with the hugging/compression priorities but I can't make any headway.
Each item is in its own stack view. What is the best way to go about this?
Layout Image
Did you add leading and trailing constraints to ImageView? As I understand you set the sizes to pickerViews?
Anyway, could you tell all constraints you have added? And how your elements should be resized on different devices(how can be compressed if there is not enough space)

Alignment using Constraints

I am using constraints Space between the green-view and blue-view changed depend upon the screen size. I am using Vertical Spacing between green-view and blue-view space is equal for all screens. but I exactly displayed Space between green-view and blue-view changes depends upon the screen sizes using constraints.
How Can i fix the problem?
From what i could understand from your problem, i am giving you below 4 screenshots containing constraints to all 3 views respectively. Last screenshot contains constraints on 1st view's proportional width.

Xcode Constraints causing image to enlarge to 1920 x 1920

Here's a screenshot of the problem:
How it appears in Xcode:
It's got content compression resistance priority of h:750 v:750, content hugging priority of h:251 v:251, trailing / leading space of 5, width and height equals to 390, top space of 40, and a 1:1 ratio.
EDIT: I'm using Auto-layout.
There are other elements that rely on it, but I'd rather get it displaying properly first.
Here's how it looks like in the iPhone 6 Simulator:
You have too many constraints set which conflict with each other. In particular fixing the width and the leading and trailing at the same time will conflict. So will setting the height and the top/bottom conflict. You are probably getting an error in the console telling you it has had to break some constraints.
If you want the image positioned as per your post and to grow with 1:1 to fill either width or height all you should need is:
Set the leading constraint to 5.
Set the top constraint to 40.
Set the trailing to be >= 5.
Set the bottom to be >= 5.
Set the aspect ration to be 1:1
This is saying that you want the image 40 from the top, and a margin of at least 5 on the right and bottom and an aspect of 1:1. The image view will grow to fit based on the constraint which limits its growth: normally the right in portrait or the bottom in landscape.
You should not set the width or height unless you want to fix or limit it. However if you do that you can not set trailing or bottom.
The image inside will show based on how you have set the image view to present the image: apsect fit, aspect fill etc...
Updated
I just noticed you have the image in a scroll view. I think you need to put the image view in a view which you give a defined size then add that to the scroll view. You may need to do that in code to deal with multiple devices. The scroll view needs to know the content size of the content view. See this tech note: https://developer.apple.com/library/ios/technotes/tn2154/_index.html

Resources