Issue with iOS storyboard view constraints - ios

i am having an issue with storyboard constraints, i have a map view, which i would like to have a variable height, depending on the screen size, e.g. (iPhone 4 & iPhone 5). The buttom section should contract and the mapkit height should contract with it. I am having a hard time understanding the constraints system, i have tried setting distance top & distance bottom to 0 & 0, i would expect that this gave the view a variable height, but it does not seem to be the case.
And this is how it looks on the iPhone 5, which is how its supposed to look.
EDIT 1

I have read your question 3 times and am unsure of what you try to achieve.
So, the result is okay for iPhone5 but not for iPhone4. What do you want exactly to achieve for iPhone4? having the same height for both sections 1 and 2, and only having the height of section 3 decrease?
If so, it is quite easy:
- Put a height constraint for section 1 (height=X)
- Put a height constraint for section 2 (height=Y)
- Put a distance constraint between 3.Top and 2.Bottom (=0)
- Put a distance constraint between 3.Bottom and Superview.bottom (or Layout) (=0)

Try selecting your view controller (not the view!) in Interface Builder, at the utilities part select attributes inspector, and try toggling the checkboxes at the Extend Edges part. Additionaly, at your screenshot I see a red arrow symbol indicating that the system cannot satisfy the layout constraints, before all, make sure you satisfy all the constraints.

Related

How to setup constraints in storyboard with autolayout for dynamic position on different device sizes

i have a question about how to setup constraints in a storyboard with autolayout if I want to guarantee multiscreen support so that all UI-controls are accessible and the UI is not messed up on smaller screen-size devices (IPhone 4s or IPhone SE) when I designing on a bigger screen-size device (IPhone 6). I have a design which is build up on a IPhone 6 (in a sketch-file) which I want to transfer to my storyboard. So in general I want to setup all the view stuff in a storyboard and not via code.
Here are my constraints. The button to the bottom has fixed height of 48 and a leadingMargin to the right and left and a vertical distance of 100 to the bottom. The top button has the equal height to the bottom button and a distance of 28 to the bottom button.
Here is the designed View with the mentioned buttons on an IPhone 6. This is the default size where I want to setup the storyboard cause of the sketch file.
Here is the View on an IPhone 4s
As you can see the buttons are to close to the middle cause of the fixed vertical distance and the fixed height. I mean this is obviously cause of the fixed values. So I made some research about percentage position like mentioned here but is this the correct way? Also other ways looks so complicated to me. How can I prevent fixed values like the height and the vertical distance? Is there a way to set the fixed height of 48 for the IPhone 6 and then scale the button down (for IPhone SE) in regards to the screen size (the same for the bottom vertical distance and other fixed values)?
Can someone give me any advice how I can proceed here?
It is a bit complicated I try to explain it as much as I can.
Note: Auto-layout is just like constructing a building every thing step by step.
Select top button and add these constraints (Leading:8,Trailing:8, And select Aspect Ratio checkbox) + And also add "Horizontal center and Vertical center constraints" select the vertical center constraint and change it's multiplier to 1.5 or increase or decrease it's value accordingly.
There should no error after this only warnings if any.
Select second button and add Top constraint to 8. Select both buttons and add constraints which are highlighted in screen short.
Run and check on different screens. Hope it help.
bottomButton.bottom = superview.bottom * 0.9
OR
Change the bottom constraint by code
bottomConstraint.constant = xx
self.view.layoutIfNeeded()

Auto Layout Challenge: Please tell me what is wrong in this case

I've been trying to understand Auto Layout, but keep failing and need help.
In this test project below I wanted to align 4 squares to look good in both portrait and landscape modes on all devices. I need to know what constraints I am doing wrong.
I used views in this case.
In the first step I added equal width/size pins since all the squares were the same size.
Size of the squares were W:250 H:121. The view container was the inferred option
In the second step I added width and height pins (cushions) between the squares.
In the third step I added all the necessary outside constraints from the individual squares to the superview (leading,trailing,top to top layout, bottom to bottom layout).
And finally what my constraints end up doing; they look fine in portrait but look weird/don't show in landscape.
What am I doing wrong? Is it a a size class issue, a constraint issue, am I completely missing certain steps, or something else?
In your 3.5 and 4 inch it doesnt show because you fixed the constraint to the bottom of your superView, that constraint's constant value alone is higher than the screen itself, forcing your views's height become 0 (or very small on bigger 5.5 screen like in your screenshot), for fixing this, try use less the CONSTANT value, instead, make the constant become 0, and change the Multiplier value on those you want to make it scale with the super view (in your case is the constraint to bottom)
Another option is change the priority of the bottom constraint to lower, and give one of the view a height constraint (Relation set to either equal or greater than or equal) and it will work

Swift - Why ScrollView not full screen?

I had inserted a ScrollView into UIViewController and dragged ScrollView to fill the space between the navigation bar and the RAM label below:
But when I run the app, the ScrollView does not fill the space:
Please help me! Thank you very much.
P/S: Sorry for my english is bad.
The scroll view is not covering up the whole thing because you are running the app on a much bigger phone. The simplest solution is to run the app on iPhone 5.
However, if you want to solve the problem on all sizes of iOS device, you need to add constraints.
Constraints are things that tells a view when and how much it should resize and where it should be positioned.
To add a constraint, just select the view you wish to add a constraint to and go to the bottom right corner. You will see 4 buttons:
The leftmost button is used to embedding views in stack views. This is a feature of iOS 9. If your deployment target is lower, just ignore it.
The second button to the left is for adding constraints related to alignment - where the edges of the views are, what its baseline is and where it is positioned in the X and Y axes:
The third button to the left is used to add constraint related to margins, width, height and how the width and height should change when it is asked to resize (keep the aspect ratio, for example):
The rightmost button is used to let Xcode decide what constraints you should add. And I think most of the times its choices are okay. Sometimes though, you still need to do some tweaking before it works.
"So... what constraints should I add?" you asked.
Well, I think I should teach you how to think when you want to add a constraint. This way, you can figure it out yourself in the future.
You should first let Xcode guess what constraints you want. Just click the rightmost button and click "Reset to Suggested Constraints". This can save a lot of work if Xcode can get it right. So remember to always do this first.
Then, run your app on various devices and see if the view's position, size, and alignment are as you expected. If it is not, you might have to add and/or remove some constraints.
For example, if you found that your view is always the same size on different devices, (that could be bad because it means that some content my go out of view on smaller devices) it's probably because Xcode added a width and/or height constraint to the view. You should delete that so that the view's width and/or height is not fixed.
You can find your view's constraints in the view hierarchy:
Just select the constraint and press delete.
Uncheck Adjust subview option and add
scrollview.view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
You need to add constraints for your scrollView. Set the leading and trailing constraints to 0. Pin the height of your scrollView and also don't forget to set the top layout constraint. You can either pin the height or add bottom layout constraint to your page control.
Constraints are very important and its even more important to set it correctly. Check the Apple Documentation - Working with constraints in IB
Uncheck constrain to margins and add 0 every one of the four limits of spacing to nearest neighbour.
My guess (from the little information we have) is that you are creating a constraint from your scrollview to the top of your view with a value equal to the height of the navigation bar. Set the value of this constraint to 0.
Just set the 4 constraints to 0 to the area you need and then uncheck the "Content Layout Guides" checkbox in the constraints tab here. It will automatically adjust to the area you have specified.
The checkbox to uncheck

Regular / Regular position sizing constraints for all iPads, including iPad pro?

I've used multipliers in my constraints, to size my views. However in my constraints for position I've used values, which I change for different sizing classes.
However using regular / regular sizing classes for iPad Pro it's positions values aren't big enough, but for other iPads it's fine.
I seriously don't have to add positioning views with multipliers to provide percentage spaces for all my views as well do I?
Right, you don't have to use multipliers because Any value other than
1 creates a proportional constraint. For a Width attribute, for
example, the width of the first item can be set to be twice the
width of the second item
First thing I like to say that the resolution of ipad and ipad pro have abundant difference so in case of yours to add positioning views I just suggest you to use size class and use of priority, constant and relation (less than or equal, equal, greater than or equal) if necessary.
Hope, It'll helpful for you...
What we're after is to set the margin between the top of the screen and the top of the subview with relation to the size of the screen. This means the constraint must be related to the superview's height.
Trying to set the distance between the two views (In the traditional sense) is actually a bad idea in this case. Instead, try this.
Add a constraint to center your view vertically in the superview. Now, open up the constraint in the size inspector (The right toolbar).
If you set the multiplier to 0.01 (0 just puts it back in the center of the screen), your view will go past the top of the screen. Change the first item from Center Y to Top. Now your view will be at the top!
From here, you can change the constant to move your view down a fixed amount and change the multiplier to move your view down further proportionally to the screen's height. The formula would look like
subViewYPosition = constant + (superviewHeight * multiplier)
You will need to adjust the constant and multiplier to suit your needs, but I believe this is a good solution to the problem.
An example of this is shown below.

Keep correct view.frame.origin.y with autolayout

I'm trying to start using auto layout consistently in my projects but I can't get my head around it.
Here I have a UIView with 2 subviews:
I want it to stay like this in retina 4 inch, but I want to move the view.frame.origin.y 40 or 60 points up on the 3.5 inch screen. Is there any way to do it with standard auto layout constraints?
Obviously, not using autolayout is not an option here.
I've tried putting different constraints up & down but they don't seem to give me the full control over the y values of the frame.
I believe you need to use the UIViewController's topLayoutGuide. Please check out the Apple's Technical Q&A QA1797 "Preventing the Status Bar from Covering Your Views", hope this is exactly what you need.
UPDATE
Sorry for not careful reading your question. I don't think you can have shift of y-coordinate by some arbitrary value for your view without runtime calculations. But you can achieve what you need by combination of the 2 constraints and modification of constraint values in the viewDidLoad method:
Control-drag from your label (which needs to be shifted up on 3.5" screen) to the bottomLayoutGuide and set vertical spacing constraint.
Control-drag from your label to itself and set height constraint for the label.
This gives you 88 pixels shift on 3.5" screen comparing to 4" screen. In the viewDidLoad, you can detect if device has 3.5" screen and increase the 1st constraint value by 20/40/60.
This is not a graceful solution but hope it helps.

Resources