Swift - UIButton is not pinning to the bottom of the screen - ios

I am having trouble pinning the UIButton to the bottom of my UIViewController, (There is a tab bar if that helps).
Here are the constraints of the UIButton.
Originally, it all worked fine, but ever since I added iPad support to the application I have issues. on the iPad the UIButton is pinned to the bottom of the screen, directly above the tab bar. However on iPhone, there is this empty gap at the bottom.
What I Tried
Doing some research I saw that sometimes split view controller causes a grey bar, I added this line of code to resolve it and it fixed the bottom bar , but this constraint issues is still causing me a headache.
self.extendedLayoutIncludesOpaqueBars = true
iPad View Hierarchy
iPhone View Hierarchy

You have an option on Xcode to vary your constraint depending of the size of the screen.
You can create two constraints, one that works on iPad, one that works on "classic" iPhone.
Here, if I create a new constraint during the variation, I can then select on the right window which variation you want the constraint to be installed on.
There is a whole topic about that here

Remove current Constrain and Try unchecking "Constrain to margins" button.

Try setting a constraint to a View instead of a SafeArea

1- Give it a constant height for example 40
2- While setting constraints, uncheck (Constrain to margins)
3- Give it a proportional width constraint with the super view 0.8
Note1: If you don't want to give it a fixed height you can do the same step 3 but with height and not 0.8 but maybe 0.2 to less depending on your design.
Note2: You can use a scroll view, it will make it so easy for you

Related

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

16 pixels margin left and right on UIView created with autolayout

I use autolayouts not for a long time and they often confuse me.
I have a wired constraints behaviour. I want to create a red view and make its margins 1/1/1/1. I tap a corresponded icon in the storyboard and set all fourth constraints to 1.
First of all values for left and right constraints are -16 and it's very strange. I have no idea where Xcode takes them.
I set all constraints to 1 and run my application. On the screen I see the view with top and bottom margins equal to 1 and right and left equal to 15.
And I have this behaviour always in every project. It very confuses me and don't give make UI correctly.
Does anybody know how can I avoid it? Everything I need it's say "Hey, Xcode, set margins for this view to 1/1/1/1" and the visible result will be the view with margins 1/1/1/1.
As usual, just after posing a question I've found a solution. When you set constrains turn the "Constrain to margins" checkbox off.

Why auto layout constraints not working?

I'm setting constraints in IB like
But when i run the app on iPhone4 it shows
Why last three buttons not shown properly on iPhone4 screen?is there any mistake in my constraints ?
Edit:- This is my view hierarchy:-
UITabbarControlle -> UINavigationController -> UIViewController and tab bar controller is rootviewcontroller.
Edit:- FYI there is no effect of any constraints on the screen(basically autolayout not working for me).
You have designed the buttons on 4 inch and you don't have enough space on 3.5 for all of them.
One way to do this is to place scroll view over your view controller's view and add the buttons on the scroll view.You have to add constraint on the scroll view , to be fixed on each side (without width/height).
After that grab all the buttons and add constrains on each side and sizes , so they will be fixed
If you , on other hand, have enough space for all the buttons to fit in 3.5' without any top/bottom margins you can add the button in UIView (instead of scroll view). Place the buttons in the view and set fixed width/height on that view.Center the view and it will keep his size (with the buttons inside on both screen sizes)
There is an issue in your constraints, you have to update them.
You can do as is shown on the image bellow:
Once done, you will see, the Orange lines under your buttons turned to blue.
I wish that helps you!
Your view is probably within a UIScrollView which will extend your total contentheight. It is not related to the absolute height of your viewport.
However, what do you expect? Your 3 buttons would overlay to the next buttons, if your theoretical approach would work.
What you need to to do is to wire all buttons to each other and give the view a top and bottom constraint to the top and bottom layout. You probably need to adjust your view to be on full screen and not to extend to the bottom.
Hint: as long as something is 'yellow' you need to add further constraints.

ios- Autolayout- add constraint respect to navigation item

I have just started to move my application to autolayout.
I want to give constraint to a label '0' pixel below navigation bar.
I tried but did not found solution.
Here are screen shots.
In storyboard I gave constraint from superview.
In IOS7 it works fine.
But in IOS6 it goes below navigation bar by 64 pixels, as superview starts below navigation bar.
If I could add top constraint for label below navigation bar it will solve the problem. But I cant find how to do it. I searched through few questions. but dint got the answer.
Please help.
Update for iOS 11:
iOS 11 introduces the concept of safe areas, and the burden of container all the content in a visible area is, again, at the hands of the view, rather than a tango of view and controller.
See Apple's documentation here.
When working with storyboards or XIBs, it's as simple as enabling the feature in Xcode. This is backward compatible and should work as expected under iOS 10 and below. In code, you would use if (#available(iOS 11.0, *)) in order to decide whether to set constraints against the safe area guide/anchors or use the legacy guides on older systems.
When using storyboards, you need to add a constraint to the top layout guide. This will take care of your issues, as on iOS6, that is 0pt and on iOS7, it is dynamic. If you are using a nib/xib file, it gets a little more complicated; you need to add the constraint in code. The easiest way to achieve this is to create a constraint with 0 vertical space to top, create an outlet to this constraint in the code and in viewDidLayoutSubviews, update the constant of the constraint outlet to the value of self.topLayoutGuide.length.
You just have to make some modifications in your constraints :
1) Give a height constraint to your label
2) Pin your label to leading view - Horizontal Space Constraint
3) Add a Vertical Space Constraint
3rd step has to be careful [I'm attaching an image for your reference.]
And after doing that, check if the red arrow mark still persist on your View Controller.

iOS Storyboard AutoLayout how to place UIView at the bottom in the both of 3.5" and 4" screens?

I want to place black UIView at the bottom in the both of 3.5" and 4" screens using AutoLayout.
I added bottom constraints on the black UIView, but the view was always hidden in 3.5" screen.
I will never use AutoLayout in next project, but in current project, how can I fix it?
Following is parent view's constraints.
Edit: Select black view press add new constraint button check height and click only buttom constraint and add these two. After that press add missing constraints button. It should solve your issue.
By the looks of things it is the constraints on the superview that are the issue. This looks to result in the superview being too high on a 3.5 screen.
Check the superview constraints and ensure that:
The height is not pinned
The top and bottom of the view are pinned to appropriate things
Check the 'document outline' and see if it lists any layout issues - correct them. Each view needs to tell the layouting system where is should be placed and how big it should be.
p.s. you should use auto-layout in your future projects

Resources