UIView-Encapsulated-Layout-Height and Container View - ios

I have UIViewController 1 , that has scroll view. Inside this scrollview there is container view that pinned to top/bottom leading/trailing (without fixed height). Container view has UITableView pinned to top/bottom trailing/leading and height constraint with 0 constant, that will change in updateViewConstraints to content size height.
When View of UIViewController 1 appears, Container View has constraint:
NSLayoutConstraint:0x7b03e5f0 V:[UITableView:0x7c42a200(54)],
NSLayoutConstraint:0x7b0ba120 V:|-(0)-[UITableView:0x7c42a200] (Names: '|':UIView:0x7b0b7000 ),
NSLayoutConstraint:0x7b0ba1b0 V:[UITableView:0x7c42a200]-(0)-| (Names: '|':UIView:0x7b0b7000 ),
NSLayoutConstraint:0x7b65f900 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7b0b7000(0)]
Will attempt to recover by breaking constraint
NSLayoutConstraint:0x7b03e5f0 V:[UITableView:0x7c42a200(54)]
What is UIView-Encapsulated-Layout-Height? How can i skip it?Because it breaks "right" constraint(that i update to content size height).Thanks

Finally i solve this problem.
1. In your tableView setting, set: tableView.estimatedRowHeight = 100.
2. In the last view of your cell, set: make.bottom.equalTo(contentView).priority(999).
3. Run your code, maybe it's ok!

Finally i found a problem. View that is added as subview to container view has translatesAutoresizingMaskIntoConstraints = YES, so NSLayoutConstraint:0x7b65f900 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7b0b7000(0)] was appeared and made me some problems. Add runtime attribute translatesAutoresizingMaskIntoConstraints = NO and it'll fix the issue.

Chiming in with one of the easiest ways to solve this: Just lower the priority of the autolayout rule that is bound to the bottom edge of your container view.
Here's my container:
I'm telling that image view to follow the bottom edge of the container, and that was giving me a conflict. To fix it, I can just lower the priority of that bottom edge rule:
I say "bottom" because in my estimation that's probably the one you'll have a conflict with, but it could be any edge that implicitly sets a width on your container.

Rather than manually setting translatesAutoresizingMaskIntoConstraints on the contentView of the cell or manually adjusting the height of this view, you should just set estimatedRowHeight and rowHeight for the tableview, as discussed in WWDC 2014 video What's New in Table and Collection Views:
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.estimatedRowHeight = 44;
self.tableView.rowHeight = UITableViewAutomaticDimension;
}
Then the fully qualified constraints you apply to that cell will adjust the row height and eliminate those annoying warnings.

Go on the item's container's xib -> deselect "Autoresize Subviews" under the "drawing" section. It fixed it for me. (The item is the item that's giving you the constraints conflict)

These issues occurs when there is a clash between the constraint.
In your case, you have already given top/bottom leading/trailing constraint to inner table view,which satisfy required constraint for table view, so there is no need of providing one more constraint (fixed height constraint with constant 0) to it.
NSLayoutConstraint:0x7b03e5f0 V:[UITableView:0x7c42a200(54)], NSLayoutConstraint:0x7b0ba120 V:|-(0)-[UITableView:0x7c42a200]
above two line means there is some clash in vertical direction for table view which has constant 0.
Two ways you can resolve this,
1 - Remove height constraint, set bottom constraint to zero initailly. Take an outlet of bottom constraint and set bottom constraint value dynamically.
2 - Remove bottom space constraint, set height constraint to zero. Take an outlet of height constraint and set height of table view dynamically.
Hope this will help you.

Set the container view's height constraint. Create an IBOutlet of said height constraint call it "containerViewHeightConstraint" or something. When you update the table view's height constraint to 54, say something like:
self.containerViewHeightConstraint.constant = 54.0;

Related

Xcode ScrollView doesn't scroll

I'm trying to implement scrollview using latest xcode.
I have content that reaches 'out of bounds' and when I run the simulator, I'm unable to scroll down to see the rest of my content.
My hierarchy / constraints are as follows..
Are there constraints you need to have on the subviews within the 'contentViiew' ? I don't have additional code in my view controller that manipulates the scrollview.
using Xcode -v 11 & Swift -v 5 <- if this makes a difference
Please clear all constraints on contentView and proceed as follows. You need the following constraints for a vertically scrolling scrollView:
1) Constraint your ContentView to have zero distance from all over sides of scrollView.
2) Equal height and width constraint to scrollView. The height constraint to have lower priority (say 400).
The only constraint for subviews is that they all have constraints set relative to view just above them. Also, the bottom subview should have a bottom constraint for scrollView to scroll.
What I mean by this is that all UIViews should have relative vertical constraints. for example, if going from top to bottom, you have three views - A->B->C, then following constraints are required -
1) top to A
2) A to B
3) B to C
and most importantly
4) C to bottom.
If you get an error in interface builder, you can use ">=" instead of "=" constraint for the #4. I hope that helps.
Add a dummy view(UIView) inside a scroll view with zero width. Set constraint of this view like this.
My dummyView Top constraint = scroll view top constraint
My dummyView bottom constraint = scrollView Bottom Constraint
My dummyView leading Constraint = scrollView Leading Constraint
My dummyView Height constraint = give height according to your need (eg 1500)
My dummyView width constraint = 0
Now whenever you want to change height of scroll view just change height of dummy view.

Auto Layout constraint is not maintained when changing the height of the view

I am fairly new to iOS app design. I have a container view (let's say conView) and a Label (let's say nameLabel) on a storyboard ViewController. I have set the top space constraint for nameLabel to be 20 point below conView. Now, when I changed the height for conView programmatically, the top space constraint for nameLabel is not maintained properly. I have used this code to change the height.
conView.frame.size.height = 200
What am I missing here? What will be the correct way to achieve the desired output?
Changing the frame of a view breaks the layout constraints and forces the view to change the dimensions or position. What you have to do is update the constant of the constraint instead of changing the frame of the container view.
Let's say you have a constraint defining the height of the container view. Bind the constraint to the view controller(constHeight)
constHeight.constant = 200
If you do not have a height constraint defined for container view, update the available constraints to achieve the height you need.
This will update the height constraint. now we have to tell it that the constraints have been changed and it should update the layouts.
self.view.layoutIfNeeded()
Hope this will help.

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.

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.

Autolayout TableView Issue

I'm having a strange issue with my tableview, debugging the view hierarchy i'm getting this
I've tried a lot of constraints with no result, how should I setup my tableview to avoid that kind of misplacement ?
Edit
I've a custom cell with an image view, actually set just this 5 constraints
Top, bottom, leading and trailing values are 0
You just need to set Your UITableView constraint to Leading,Trailing, Top and Bottom. and also for your UIImageView set same constraint. so your imageview will show fit to cell as per table width. no need to set Align Center X constraint.

Resources