Proportional distance to top in autolayout and device rotation - ios

I have an UIImageView in an iPAd Screen. The three Auto Constrains are
1) centered horizontally in the container
2) fixed width
3) fixed height
I have another constrain which tells the image view to be 100 points under the top of the screen.
If the device goes to landscape mode, I want to have this value 70 (the numbers 100 and 70 are imaginary, can be others, relations, proportions etc.)
What should I do for this without solving it programmatically, pure auto layout in the storyboard interface builder.
Thanks for helps.

Use the multiplier of a constraint, accessible by selecting the constraint in the XIB.

Related

How to scale buttons for different devices

I have buttons on my screen I have attached constraints but on different sizes of screen I don't see my buttons right as I want to. There are the same size on different screens and I can't see all buttons on small screens
Here is a good version of it on iphone 8 plus in xcode
and then I change view to iphone se or any another iphone and I get these troubles:
What do I need to do to have the same screens?
Make the buttons have a width that is in proportion to the view
width.
Then add a 1:1 aspect ratio constant so that the buttons remain
square.
Then position the buttons in relation to the screen dimensions (not
absolutely).
This should get the job done for you.
Setting proportional width
Suppose the main view's dimension is 375 x 667 in interface builder. Suppose you place a 90 x 90 button on it. To make it always have a width in proportion to the main view's width,
Add an equal widths constraint to the superview for the button.
Edit the constraint's multiplier: Change it from 1 to (90/375)
Doing the above makes sure that for different screen widths, the button's width will also change in proportion.
Here are screen shots from iPhone XR and iPhone 5s, that I took from a sample I just did to try this out. As you can see, the buttons stay within the screen and maintain their shape. Hope this helps.
You don't seem to have defined the constraints of the button to superview. eg: left edge, top edge...
*edit: post complaint below #raisedeybrow
You will need to define (add constraints) how far from the edge (left and top, right and bottom) you want your button/s to be. Then apply content hugging and compression priorities on buttons for all of them to scale as you'd like. Some will need to be bigger, some you don't want to allow to shrink etc. Lots to set if you'd want it to look fluid

how to set autolayout for different devices by using storyboard

I have an Iceland map which contains 9 different area buttons. Every button has a different size. I want the map can show the same on different devices. I only know to set the central button with horizontally in container and vertically in container. However, other buttons, no matter how I set the constraints, they will be a mess on iphone SE or iPhone Plus. (I use iphone 8 as normal)
Can anyone teach me how to set the constraints for the 8 left buttons? thank you!
The best thing for this is a vertical UIStackView nested with 3 horizontal stacks
MainStackView constraints
centered vertically & horizentally , width & height propotional to screen
then drop 3 stackViews inside it set axis = horizontal and drop 3 Buttons for every inner stack
Note: distribution is fillEqually for all the stacks , spacing = 10
look to this Demo
Something I've done in the past is used a single control as the anchor for all the others.
Basically this means taking the centre button and anchoring it to the middle of the container view (centre horizontally and vertically)
I then constrain the middle/top and bottom controls to it (vertical space and horizontally centred)
From there each control in the row is then constrained against these elements (horizontal spacing and vertically centred)
You might also consider constraining the sizes to the centre control as well, so all the controls share the same size. For me, this means constraining the width of the control to a set value and then applying an aspect ratio to the height. I do this because then I can change the value of the width constraint and ALL the control will change size
Because iPhone 5s, iPhone, iPhone+, iPhone X all share the same "sizing classes", it makes it some what more difficult (as you can't apply traits - but you can do it for iPad and iPhone ;)).
At this point, I would bind the centre controls width constraint (and the height if you need it) to the source code and when the view is loaded, determine the device screen size and make minor adjustments to the constraints values.
If you would prefer a complete "storyboard" solution, you could constraint the width of the centre control to the super view and apply a modifier
Using the "device" templates from the storyboard, layout one a iPhone 5s and iPad Pro
The long and short of it is, you have "options". You might even consider using UIStackView to define the rows, personally I find it easier to constraint the controls to a central anchor point - but that's my experience and your needs might differ

Auto layout issue for devices in the same family and orientation

I'm working on my beginner app and checked on the preview option for my app. I've noticed that across the iPhone X, 8+, 8 and SE the spacing of my objects are off. I've learned about the custom size class for various devices for different orientations but nothing what said about the same device family in the same orientations (ex: iPhones in portrait mode). This is what I am seeing.
The iPhone X looks fine but the others, not so much. I'm guessing that instead of using hard numbers for auto layout I should be using a ratio based on the height and width relative to the screen. Is that right?
Try to take Base ViewController as iPhone4S. Drag UIView called as HolderView, which should hold that UILabel and Two UIViews.
As per Design, give HolderView's width and height be same. Like Square shape.
Give Constraints as, left and right as 20px, CenterHorizontally and CenterVertically, and finally give AspectRatio to itself.
HolderView
Add UIView for UILabel to HolderView called as LabelHolderView.
Give constraints for LabelHolderView as left and right 20px, CenterVertically and Height as 90px (approx - Two lines for UILabel)
Click CenterVertically constraints and decrease Multiplier value to 0.4.
You can get answer from here that how to change multiplier value.
LabelHolderView
Add UIView for Two Action to HolderView called as ActionHolderView.
Give constraints for ActionHolderView as left and right 20px, CenterVertically and Height as 160px (approx)
Click CenterVertically constraints and increase Multiplier value to 1.3.
Inside that ActionHolderView, drag Two UIView called as PhotoView and NoteView and Embed in UIStackView as Horizontal axis, Fill Equally and 8 as Spacing.
Give constraints for UIStackView as top, left, right and bottom as 0.
ActionHolderView
Change all UIView's BGColor as per your design. Then, add UILabel and UIButton and UIImageView to respective UIViews.
For UILabel, top, right, left and bottom as 0. (number Of lines as 2)
For Two UIButtons ,right, left and bottom as 8 and height as 30
For UImageView, Width and Height be 50px (Change width and height from Size Inspector - Square shape). Give Constraints as CenterHorizontally, CenterVertically and AspectRatio to its SuperView and AspectRatio to itself
ViewController
You need to set height/width constraints for your items. I would also put your add photo/add note buttons in a view to make it easier for constraints. Don't be afraid to mess around with it, you can always to command z to restore the previous version.

autolayout view gets overlapped on smaller devices

Basically I have one view with some height x
and 4 other views with same height y all these 5 views are vertically one after another with 10 px space
all these views have again some child views which depends on them.
For this scenario stackview is ideal but it support starts from iOS 9 unfortunately I have to support from iOS 6
so basically I fixed all the views with leading,trailing,top,bottom and height constraint...
it works good in iPhone 7s ... but in iphone 4s the view gets overlapped on each other
in iphone se the views are very much congested..
I was always thinking the height will change according to the screen size for iPHone 4s but that didn't happen
How can I work this out ?
There are two ways to solve the problem you are facing.
First:
Instead of giving a fixed height to any of your UIView you can use a proportional height constraint. Select your UIView and control + drag from your UIView to the main superview. Select equal height constraint. Now, double click the height constraint and set the multiplier to 0.15 or any value you seem nice. This will ensure the view thus created is always 0.15 times the entire view's height. Now you can create your other views either proportional to this view following the same steps or to the superview.
Second:
(I prefer this approach as for items as sometimes you need to create forms the above approach might still push elements off the screen).
This will use a UIScrollView. To your main view add a UIScrollView and add a leading, trailing, top and bottom constraint to your superview. Add a UIView to this UIScrollView and give it a leading, trailing, top and bottom as well. Additionally give it a equal height and width constraints to the superview of the UIScrollView with a low priority of like say 250. Now add all your elements inside this UIView however you seem fit. With fixed height, proportional height whatever and happily run it. But ensure you add a bottom of >= a minimum value for your bottom most view.
The screen will automatically become scrollable if the content will go off screen otherwise it won't be scrollable at all.
fixed all the views with leading,trailing,top,bottom and equal height constraint instead of only height(not give fixed height to any view)

iOS view height and auto layout

I have a viewController, which is configured for the iPhone5 screen size. In red rectangle is the view.
To make this view show properly in iPhone4 - I used auto layout. But when I specify constrains such as: leading edge, trailing edge, bottom space and top space (in top space I even made constraint's priority to LOW ) - the view still goes partially down the screen as if my constraints don't work.
BUT if instead of top space constraint I specify view's heigh and delete top space constraint - everything works perfectly.
Does anyone can explain it please? Thank you.
Thats because when you set up the top space constraint it will move the view by the constant you provided. iPhone 4 and iPhone 5 screen height is different but the constant remains the same so obviously it will behave differently. One way to troubleshoot your interfaces is to switching between iPhone4 and iPhone 5 on storyboard device on storyboard (first button from left to right on the bottom right corner of interface builder).
Auto-layout is all about experience in my opinion. I struggled alot with it until i learned. If you want your view to be attached to the bottom of the screen you should set the BOTTOM SPACE to 0 and specify the view's height like you did or adjust constraints for it's subviews so that the height is set dinamically according to the views inside.

Resources