No fixed height in interface builder, constraint predicament - ios

The screen I'm trying to create is very simple. I have two UIViews stacked atop one another. The top UIView, topView, has height of 40px, and the UIView below, botView, takes up the rest of the screen. However, sometimes topView should not appear and I want botView to take up the entirety of the screen.
What I've tried is setting width, leading/trailing space on both views. Then topView gets distance to top layout guide, botView gets distance to bottom layout guide, and then an additional constraint for vertical spacing between topView and botView. But this results in xcode yelling at me that I haven't set a height or y constraint on one of the views. Of course, I can't set a height constraint because I do not want either of them to be fixed height.
I'm sure there must be an elegant constraint solution here that I just can't seem to figure out. Thoughts?

If topView should always be 40px height, then add a constraint for the height of topView.
As for botView, you can keep the current constraint that says its top has to be correlated with the bottom of topView. But add another constraint for bottom view with a priority lower than required that says it should be 0px away from the top of its superview. This way, when you remove topView from the containing view, botView's constraint related to topView will be deleted, and therefore will satisfy the lower priority constraint.
And if you want to push back topView in, recreate the botView to topView distant constraint with required priority.

There are a couple of ways to do this. One option
In IB lay out your views. Then pin top view 0 px from the top, bottom, left, and right. Specify a height constraint of 40 px as well. To easily do this click on your view, then in the lower right hand corner click on the button that looks like | + |. Click on the dotted red lines to specify the constraints. See image:
Do the same for the other view. Don't specify a height for this view because it's height is dependent on the 40 px view for its height. Next find the height constraint for the 40 px view. You can can use the assistant editor to control drag an outlet to you view controller just like you would with a UIButton or any other IB element. When you want to hide your 40 px view set the variables constant property to 0. When you want to show it again set it back to 40.
self.topViewHeightConstraint.constant = 0;
The other option you could go with is to pin the top view 0 px to the top, left and right. Don't provide a bottom constraint but specify the height as 40 px. For the bottom view add add a left, right, and bottom constraint of 0 px. For the top constraint add a constraint of 40 px from the top superview. Again add an outlet for the most recent top constraint and when you want the view to be larger set that constraint to be 0.

An alternative: In IB, add a distance to top layout guide constraint for BOTVIEW (it will be 40...) Then click-drag this constant to the VC to create a NSLayoutConstraint property.see also here
Now, upon the condition that hides topView, you set self.myConstraint.constant = 0; This will force botView to resize to meet the condition of being 0 from the top.
You may have to clear your current constraints to get rid of errors, but this should give the desired behavior.
As for "topView" like Tom Ahh suggests, go ahead and give it a height constraint of 40 and pin it to the top as you're already doing.

Related

Need help to set autolayout

Am new to iOS & am facing it very difficult to set autolayout. Watched many videos to learn, but all of them giving solution to a specific problem. No video covers all base rules to set an UI object into it's place & with proper flow.
I came from Corona Background & used to set UI programatically very well. Am thinking here same way, but I think apple made it so difficult or people are not able to explain me properly.
Please see 2 images attached in this question & tell me rules to apply to achieve this UI. I request you people to please explain in the general manner so that my other screen can be completed using same rules.
Image 1: http://i.stack.imgur.com/MPE47.png
Image 2: http://i.stack.imgur.com/qEiCm.jpg
A really helpful guideline is
Every element should be able to figure out its position (x and y) and size (width and height).
Ensure that every element only has one way to figure out its position and size.
Remember that the autolayout of all the elements can influence each other.
The most used layout constraints are:
Top The space between the top of the view to another view.
Trailing The space between the right edge of the view to another view.
Leading The space between the left edge of the view to another
Bottom The space between the bottom edge of the view and another view
Width Assign a fix width to a view (Note that it can also be a percentage - aspect ratio)
Height Assign a fix width to a view (Can also be a percentage)
Center Horizontally Always align the view relative to the horizontal center of another view
Center Vertically Always align the view relative the the vertical center of another view
For example in your second image, say the yellow bar is a UIView called titleView.
Set the position of titleView by setting the top layout constraint to the container view. y position is set.
Set the leading constraint to the container view. x position is set.
Set the trailing constraint to the container view. The view's width will now stretch with the screen size. Thus width can now be determined.
Set height to 50. Height is set.
Now... If you also set the width of this view, it will cause the layoutConstraints to break, because you have redefined the width constraint. Some of the constraints will then be ignored.
Another example of how layoutConstraints might influence each other. Lets look at determining the y positions of the second image.
Say titleView has a top constraint to the container + height of 50.
currentCampaignView has a top constraint to the bottom of titleView. (Use vertical spacing) + equal height to titleView. (y + height can be calculated)
the 5 buttons have equal heights. Top buttons have Top space to Bottom of currentCampaignView. Centre buttons have Top space to bottom of top buttons. Bottom button have Top space to bottom of centre buttons.
startCampaignView has equal height to currentCampaignView. Top constraint to bottom of bottom button and Bottom constraint to container view.
Note that because views and buttons have equal heights, all are considered when determining the height. Thus it is very important that they are all interlinked and that the entire height that can be used is specified. In this case it is specified by the first element titleView that has a Top Constraint to the Container view (of which the height should be known) and the last element, startCampaignView, that has a Bottom constraint to the Container view. Because all the views in between are linked on y position and height, the view can work out what each view's height and y position should be.
One more example. (Your first image)
topLeftButton Set the top constraint to Superview. (y), Set the leading constraint to Superview (x), Set height = 100 (height), Set equal width to topRightButton (Note that we do not quite have the width yet, because the width of topRightButton can not be determined)
topRightButton Set the top constraint to Superview. (y), leading constraint to topLeftButton (will be used for x), Set trailing constraint to superview. Now the width of both buttons can be determined, because we have an external startX + endX and we know the two buttons touches each other and are equal widths. Thus the available space will be split to get the width of the two buttons.

Q: Auto Layout 4 squares Issue/General Auto Layout Issue

So I'm just trying to wrap my head around Auto Layout; I understand how to use constraints, just not how to apply them appropriately. In the picture below I had set no constraints and simply want this simple design to resize correctly on all size classes (Horizontally and Vertically).
http://tinypic.com/r/2ymxbop/9
What constraints would I need to put in to have these squares resize appropriately on all screens (horizontal/vertical) and can you do this in the ( W:Any H:Any ) size class or do you have to set constraints for each individual size class?
Thanks!
One size class is all you need in this case.
Set a constraint for each of the 4 gaps between the rects (i.e. the space between the top 2, between the top-left and bottom-left, ...).
Set a constraint for each rect to its 2 closest outer edges.
Now you've set four constraints for each rect - and that's all you need!
For that case: follow the steps
1.Add four uiview to screen, two top and two bottom.
2.In your case the view not touching to bottom layout guide,so you have to give fixed height.
Start the add constraints to views.
All views are correct width and height.
3.So take first view from left top,give leading space ,top space and adjecent spacing to right top view. And select the pin button from the canvas below right side option availabe, select height ,give how much height you need.
4.Now select right top view ,give constraint to top layout guide and trailing space.and also same height what you selected before for view one.
5.Now view three ,left bottom view , give leading ,spacing to top view and right spacing to fourth view and select same height .
6.Now fourth view, give spacing to top view ,trailing to container and select height.
Almost completed.
7.Select all views at once ,give equal withs(option available in pin).
lastly some warning will shows ,do update frames.

Autolayout Parent view height change

I have parent view at top of screen let say TOPVIEW in storyboard which contains some fixed height subview
and scrollview following TOPVIEW
Looks Like
UIVIEW
- TOPVIEW (128) FIXED
- SCROLLVIEW
When keyboard appears i want to set top view to 0 that's why i have taken IBOUTLET for same and set to 0 and top view clips bounds to YES
But constraint breaks of subviews of topview (Vertical space between views etc)
My question is how to solve constraints break due to parent view height in subview ,
i know one that we can set priority low for breaking constraint but is there any other way ?
Thanks in advance
Put the top view and the scroll view in a vertical UIStackView. When you want to hide the top view, set topView.hidden = true. The stack view will automatically move the top of the scroll view up so it fills the space where the top view was.
That is happening because you have set fixed height constraint to the TOPVIEW and you are changing that constraint constant value dynamically, so auto layout would have to break the constraint.
If you have set ideal constraints to the TOPView and ScrollView then you would just need to make two or three changes in your constraints as below,
Step 1:
Change the height constraint (128) relation from Equal to Less than or Equal as show in the image below,
Step 2:
If you have set the scrollView constraint correctly as explained in the link here, then you just have to make one change that is change the BottomSpace Constraint Relation from Equal to Greater than or Equal of the component which is at the bottom of scrollView (inside scrollView).
Step 3:
Create an outlet of Height constraint of TopView and toggle its constant value from 128 to 0 and vice versa.
That's it, it should work now as expected.
Hope it helps you.
Update
Alternatively you can also go for other way as explained below,
Step 1:
Change the bottomSpace constraint priority of component inside TOPView at the bottom of TOPView to 999.
Step 2:
Select the Clips Subviews property from attribute inspector as show in the image below,
To prove this, i have done small demonstration app and you can see the result below,
Orange View has TopSpace to SuperView, Leading and TrailingSpace to SuperView and Height constraint.
Blue View has TopSpace, Leading, TrailingSpace and BottomSpace to SuperView i.e OrangeView, BottomSpace constraint has priority of 999. And it has Height constraint.
This will do the trick.

iOS Auto layout in percentage

I wanna create a simple iOS application using autolayout that displays three UIViews. One on the top, one on the left and
the other on the right. The top view should be 50 pixels high and fills the horizontal space completely. The left column should fill 40% of the horizontal space and should fill 100% of the
vertical space. The right column should be 60% of the horizontal space and 100% of the vertical space. Finally in the left UIView wanna add a single button.
Yes, You can.
Add first view at the top, add four constraint, top 0 left 0 right 0 and height 50.
Add second view at bottom of first view, make it top 0 left 0 heigth 100.
Here is the trick, choose first and secound view, then make them equal width.
Choose secound view's width constraint, and change the multiplier to 0.4.
The two existing answers didn't work for me. My answer was built using Xcode version 10.1 (10B61).
The core question is "How Can I Set a View’s Width to be a Percentage in a Storyboard". Answer: the view's width needs to be associated with the superview.
Explanation:
In the Main.storyboard (already containing a View Controller and a View), add a View. I’m going to call it YellowView). And change its containing view name to SuperView.
In the YellowView's Attributes Inspector change the Background to Yellow.
Click on New Constraints and Top = 0, Leading (left) = 0, height = 50
Control Drag from the YellowView to the Superview and let go, in the resulting dialog select "Equal Widths"
Note: YellowView's width becomes the width of the SuperView.
In the Storyboard, Double-click the width constraint we just added and change the Multiplier to 0.25 (Note: Typing .25 will not work - the leading zero is necessary)
Your constraints should be like,
first view - top,leading(left),trailing(right) and fix height
left view - top, left(leading), bottom and equal width with superview with multiplier 0.4 (or width constraint with 0.4 multiplier)
right view - top,right(trailing),bottom and equal width with superview with multiplier 0.6 (or width constraint with 0.6 multiplier)
hope this will help :)

Equal widths / heights with parent (root) view?

I'm trying to add constraint in Storyboard to an UIImageView to it's superview, equal widths or heights but it's disabled.
I tried selecting both the view and the superview, then the add constraints button at the bottom right (mentioned constraints appear disabled), I tried dragging from view to the superview (mentioned constraints aren't listed).
I tried removing all the constrants related to my view. Didn't make a difference.
I'm using a xib, of a custom view. The parent view is also the root view. XCode 6.2 beta. Using size class Any/Any.
Any idea? Thanks.
For making UIView height/Width equals to super View.
Here's what you need to do.
You can set leading,top,tailing and bottom constraint by right clicking to View and select superview.
After setting Top and bottom constraints as i've set in previous image. You need to update constraints to '0' as it might have top and leading space not equal to '0'. You can select those constraints and changes constant to '0'.
You can also set constraints from pin to View as i've set for leading and tailing you can also set top and bottom option in similar way.
EDIT:
If you want to set width and height equally then you need to select both the view and set it's equal height and equal width. As I've set them in below image.
Result:
Below two images shows how you can changes width/ height using multiplier as you need to set constant to 0.9. as from storyboard or xib you cannot set constant in float. So you need to set multiplier.
First Image I've set multiplier in ratio.
Second Image I've set multiplier to 0.9
If you still face any queries please let me know. Hope this will help you solve your problem.
Don't give the inner view (UIImageview) any width or height
Add top margin (with superview) with 0
Add bottom margin (with superview) with 0
Add left margin (with superview) with -16
Add right margin (with superview) with -16
Well, I ended adding a parent view in between. This parent is constained to the top view - left, right, top, bottom: 0, and then I can set my inner view to equals width and hight to the parent (the reason I want to do that is to make proportional size - set constant to e.g. 0.9).
No idea why it's not possible to do this directly with the root view.

Resources