Why auto layout constraints not working? - ios

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.

Related

Autolayout with UIScrollView

I am using autolayout in Xcode and am creating a fairly tall (1300px) ViewController with a uiscrollview to navigate up and down. I made the viewcontroller freeform so its dimensions are [375,1300]. I then incorporated the scrollview, added the content view as well as all the subviews & constrained everything, leaving no constraint errors. At runtime there are no errors, but everything is all smushed into the normal screen size when I intended for it to be very tall (~1300px) and scrollable. Any clue as to what I am doing wrong?
Below is a diagram showing the issue where:
The Outer Black Blox is the ViewController's view
The Inner Black Boxes are subviews
and The Blue Box is what is displayed on the screen
Yes, your correct #Ryan Cocuzzo. I think you need to set sub view priority from 1000 to 250. See below screen shots.
1) Select height constraint of the subview
2)Then go to show to size inspector
3) Now change the priority constraint from 1000 to 250.
4)Finally you get like this
Make sure the following are true:
The 4 content views are subviews of the UIScrollView, not your view controller.
The autolayout constraints on the 4 content views must reference the UIScrollView only, not the view controller's view.
The 4th box cannot have a bottom constraint.
It looks like the 4th box has a constraint to make it's bottom align with the bottom of the view controller's view. This would force the other views to smush up together. The 4th box also cannot have a bottom constraint so that the scrollview can resize itself to encompass it's subviews.

Too much text pushes my buttons below the screen

So I tried embedding my label (that has all the text) and buttons in a scrollView to perhaps be able to scroll down to them, but when I do that, everything just disappears.
my storyboard
my preview
Just added some pictures as requested (thanks).
As for adding the scrollView (which may not be the optimal solution), I just selected all 4 buttons and the label, then selected "embed in scrollView". This led to a completely blank screen in preview.
It sounds like you need to fix the bottom constraints of your scrollview, so that the scrollview's frame does not extend past the bottom of the screen. You can then set constraints on the scrollView's content size to allow the user to scroll.
From the storyboard image you posted, it looks like there isn't a bottom constraint. Select the scrollview in the storyboard, click the pin icon, and put in a number in for the bottom constraint (e.g. 0).

Is it possible to have overlapping views with auto layout?

I'm trying to have to views overlap as shown in the picture below, but I couldn't figure out how would I go about doing it with auto layout.
I tried doing the bottom view programmatically, and it worked fine except for the button which I had laid out using interface builder.
If it's possible to have views overlap what are the constraints needed, and is there a way to let the button show on top of the view in the bottom without doing programmatically?
You can do something like this.
make the second view uplift with minus value:
Here is the output. SecondView(green) has been uplifted to -50
You can do overlapping views using InterfaceBuilder. First create the top view i.e the one with image, give its constraints like top ,trailing,leading and bottom space. Then create the overlapping view.
The constraints for the overlapping view can be its trailing space, leading space,bottom space and height.
Then add those buttons inside that view.
In the interface builder, using a constraint, hook the top of the bottom view to the bottom of the top view, and then edit the constant in the Size Inspector to move it like you want.
It should look something like this:

AutoLayout : Relative scaling of views

I want to achieve consistent views across different screens. My layout constraints works on small screens perfectly but it scrambles on large screens.
I have made a view(Red Border indicating that view)and pin it in to the main super view. Then I have made sub views and put it inside that view. I have pinned the the top bottom trailing and leading edge of the layout which contains button of the it's parent view (Red Border indicating that view). The view containing button is looking like this in small screen (iPhone 5)
while it's looking in big screens like this (iPhone 6 and above)
I want this layout to have the same relative height in all the screens like the other views. How can I achieve this?
I guess you can use UIView that contains a button inside and set bottom,lead,trailing,top constraints for the UIView. Then you can set height constraint of that UIView and set multiplier values to have relative height to its superView(=Red box). Finally, you can set constraints for your button inside the UIView whatever you want. If you know how to use StackView, I recommend you to use it because it is easy and simple solution. Here is very good tutorial about StackView.
If height is constant for subview except that button view the layout will be like what you mentioned in the first image .If you kept height as constant for that button view the problem will be solved

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