AutoLayout issue in dynamic UITableViewCell's subviews - ios

While I seem to have taken the gist of setting the view constraints on UIViews of storyboards, I just can't seem to figure out why constraints are not properly working in a dynamic cell prototype ContentView's subviews.
The cell is pretty simple: A UIImageView background that fills the entire cell, in front of it another UIImage and a label as following:
The background UIImageView has the following 4 constraints:
0 fixed to: trail, bottom, top and leading space to superview
The profile picture image has a fixed width and height and a constant 13pts space to leading superview and has a vertical center in container
The Label has an 8pt leading space to the profile pic and trailing space to superview, and has a vertical center in container.
This is what I am getting:
Did I miss something? I researched the issue but no answer solved the issue. However, I tried adding the following lines in the cellForRowAtIndexPath after loading the cell:
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
which gave the following errors at runtime:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"NSLayoutConstraint:0x7fe5434852c0 H:[UIImageView:0x7fe543484450(75)]",
"NSLayoutConstraint:0x7fe5436c5cd0 H:|-(12)-[UIImageView:0x7fe543484450] (Names: '|':UITableViewCellContentView:0x7fe5434820f0 )",
"NSLayoutConstraint:0x7fe5436c34e0 H:[UILabel:0x7fe543487430'Joseph']-(8)-| (Names: '|':UITableViewCellContentView:0x7fe5434820f0 )",
"NSLayoutConstraint:0x7fe5436bcca0 H:[UIImageView:0x7fe543484450]-(8)-[UILabel:0x7fe543487430'Joseph']",
"NSLayoutConstraint:0x7fe5436cb110 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x7fe5434820f0(0)]"
)
Will attempt to recover by breaking constraint
NSLayoutConstraint:0x7fe5434852c0 H:[UIImageView:0x7fe543484450(75)]
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in UIKit/UIView.h may also be helpful.
Any clues?

Well, problem is priorities.
You have background image, which has some size according to image you put into it and than there is a profile picture with fixed height and constant space 13pts top and bottom.
Table may have separator and that one is adding extra space.
So advice: lower for example bottom 13pts constrains from 1000 to 750. That should remove warning.

Related

Unable to simultaneously satisfy constraints When trying to load xib file

I am trying to implement the UITableView using xib file
But when I run the app I am getting the following error & the xib is properly in small screen like iPhone4s
it works fine in larger display size.
Screenshot of xib
Any help will be appreciated
Error
2016-12-14 17:12:41.826 FoodStrock[5776:1761262] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x7fcc2a6dfac0 V:[UIImageView:0x7fcc2a6e1010(63)]>",
"<NSLayoutConstraint:0x7fcc2a617530 V:[UIImageView:0x7fcc2a6e1010]-(8)-| (Names: '|':UITableViewCellContentView:0x7fcc2a6e1270 )>",
"<NSLayoutConstraint:0x7fcc2a6cc3b0 V:|-(9)-[UIImageView:0x7fcc2a6e1010] (Names: '|':UITableViewCellContentView:0x7fcc2a6e1270 )>",
"<NSLayoutConstraint:0x7fcc2a4955a0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fcc2a6e1270(59.6667)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fcc2a6dfac0 V:[UIImageView:0x7fcc2a6e1010(63)]>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2016-12-14 17:12:41.827 FoodStrock[5776:1761262] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x7fcc2a6a69d0 V:[UILabel:0x7fcc2a6e1670'Address'(63)]>",
"<NSLayoutConstraint:0x7fcc2a693820 UILabel:0x7fcc2a6e1670'Address'.bottom == UITableViewCellContentView:0x7fcc2a6e1270.bottomMargin>",
"<NSLayoutConstraint:0x7fcc2a6e0c00 UILabel:0x7fcc2a6e1670'Address'.top == UITableViewCellContentView:0x7fcc2a6e1270.topMargin + 1>",
"<NSLayoutConstraint:0x7fcc2a4955a0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fcc2a6e1270(59.6667)]>"
)
Put your UIImageView , UILabel, UIButton in single UIView(says mainView). Now mainView constraints will be leading, trailing, top bottom from the xib view(superView). Now constraints of UIImageView , UILabel, UIButton is according to your requirement w.r.t. mainView.
NOTE: Give constraints to the UIComponents as minimal as possible but keep mind about it full fills your requirement. Like here, give constraints like this:
UIImage : top, bottom , Leading wrt mainView and constant width or you can give horizontally center to container, leading, height, width.
UIButton : top, bottom, trailing wrt mainView and constant width or you can give horizontally center to container, leading, height, width.
UILabel : leading trailing and horizontally centre to container is enough but you can give height also.
UILabel can automatically take height and width according to the text font size you just need to specify x and y coordinates.

iOS autolayout: set constraints in viewDidLayoutSubviews?

I have a view controller with xib. xib contains all the subviews without constraints and I need to add these constraints manually. Everything is correct when I write my code in viewDidLoad. But I need to get a size parameter which is available in viewDidLayoutSubviews only. When I try to call my code in viewDidLayoutSubviews then I get the following error:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x79edebb0 V:|-(50)-[UILabel:0x79f11bf0'You haven't imported any ...'] (Names: '|':UIView:0x79f11940 )>",
"<NSLayoutConstraint:0x79e4b190 V:|-(49)-[UILabel:0x79f11bf0'You haven't imported any ...'] (Names: '|':UIView:0x79f11940 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x79edebb0 V:|-(50)-[UILabel:0x79f11bf0'You haven't imported any ...'] (Names: '|':UIView:0x79f11940 )>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
The constraints are simple: all the views are placed in one column, fixed width, fixed height, distance between topmost view and superview's top border, distances between neighbour views and finally all the views are snapped to the superview's horizontal center.
UPDATED
I found the reason of the problem but I don't know why it doesn't work.
The problem is the topmost constraint value was based on views' bounds. The height value inside this bounds structure "jumps" by 1 (because viewDidLayoutSubviews is called multiple times). But why does it cause problems with constraints?
it says that 1 of the UILabel have Constraint which breaks layout.
2 options
clear Constraint of all label and set Constraint of each label by try n error
set id to all constraint of uilabel and then u will see id of constraint in
warning or error output.
basically it is due to u added an unwanted constraint.

NSLayoutConstrains error occurs when reorder the UITableViewCell

I set a simple cell in storyboard,
Thers's a imageview and a label, I set some constrains, it's imageView's leading ,top and bottom space to contentview, and the imageView's width and height, and the label's horizontal spacing to imageview and vertically center to imageview.
When I run the app, it shows correct, no error occurs.
The rightBarButtonItem is the system edit button. When I reorder the cell, constrain error occurs, I do not know how to fix it.
2015-11-11 23:08:05.681 HiPDA[10108:2305919] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x7fce60ce6cf0 V:[UIImageView:0x7fce60cac370(40)]>",
"<NSLayoutConstraint:0x7fce60ce6f10 UITableViewCellContentView:0x7fce60ca0610.topMargin == UIImageView:0x7fce60cac370.top>",
"<NSLayoutConstraint:0x7fce60ce7020 UITableViewCellContentView:0x7fce60ca0610.bottomMargin == UIImageView:0x7fce60cac370.bottom - 0.5>",
"<NSLayoutConstraint:0x7fce60c03a70 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fce60ca0610(56)]>"
)
Will attempt to recover by breaking constraint
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
I was confused, when it showed, scrlled and removed, no errors occur, but when I reordered the cell, constrain error occurs, I do not know how to fix it. Thank you.
I upload the project, you can download here download link
There's are two navigation controllers, change the init controller between them, when the tableviewcell contains only label no error occurs, but when the tableviewcell contains imageview error occurs when reorder cell.
You gave imageView two Sets of constraints to determine it's height ,one is top&& bottom space, another is height.
When they conflicts with each other, you will get this alert.Change one of them to optional will fix the problem.
In you case ,You can change your imageView's height constraint from "=" to ">=" or lower height constraint's priority to 999.
Looking at your constraints the conflict is that
The imageview height is set to 40, it is pinned to the top and bottom of the cell, while the cell height is 56. how big do you want the imageview 40 or 56?
You can solve this by removing the top and bottom constraints and just do a center vertical to the cell constraint. i.e. ImageView.centerY = centerY
Here's modified constraints for your project on the image view

Why does adding constraints for all four sides cause an error?

UPDATE
Editing an XIB file, I added a button to the position that I'd like in the view and clicked the pin icon at the bottom right, and pinned all four sides of the button, with "Constrain to Margins" unchecked, as that's the exact location I would want to place the button and I wouldn't want it to move around.
But I got an error that says 'Unable to simultaneously satisfy constraints.' Why does that happen?
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<_UILayoutSupportConstraint:0x9f494b0 V:[_UILayoutGuide:0x9f40370(20)]>",
"<_UILayoutSupportConstraint:0x9f3b6c0 V:|-(0)-[_UILayoutGuide:0x9f40370] (Names: '|':UIView:0x9f3feb0 )>",
"<_UILayoutSupportConstraint:0x9f418e0 V:[_UILayoutGuide:0x9f407c0(0)]>",
"<_UILayoutSupportConstraint:0x9f18010 _UILayoutGuide:0x9f407c0.bottom == UIView:0x9f3feb0.bottom>",
"<NSLayoutConstraint:0x9f40f10 V:[UIButton:0x9f40f40'Button']-(211)-[_UILayoutGuide:0x9f407c0]>",
"<NSLayoutConstraint:0x9f47ab0 V:[_UILayoutGuide:0x9f40370]-(539)-[UIButton:0x9f40f40'Button']>",
"<NSLayoutConstraint:0xa157b10 'UIView-Encapsulated-Layout-Height' V:[UIView:0x9f3feb0(568)]>"
)
Will attempt to recover by breaking constraint
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
Autolayout technology work on relationship between objects and object with superview how they will draw on screen. as you mention you are trying to force object through all sides so set for specific position you can try by think how object will draw on screen by applying specific constraints.
if you want to place object of fix width and height at center of screen in all orientation then you can try
Align->Horizontal and Vertical center in container.
(Otherwise even they can crash your app if unsatisfied constraints)
Hope this will help you.
If you have a height or width assigned to the button it will conflict with the constraints you added. If you want it to always be at a certain x,y and specific size you should pin the top and left along with the width and height. If you want it to dynamically resize you should pin all 4 sides.

What is the correct way to use UIScrollView?

I tried to setup a simple view which displays a text-only article with a headline. Here are the steps that I've done:
Create View Controller with its .xib file,
Create UIScrollView and places a UIView directly as the content wrapper,
Set the constraints [scrollview]-0-[superview] in top, bottom, leading, and trailing.
Set the constraints [content wrapper]-0-[scroll view] in top, bottom, leading, and trailing.
Set Width and Height to content wrapper as placeholder.
Add Label and UITextView as content wrapper's subviews.
Add constraints to the subviews.
Following this tutorial, I programmatically set content wrapper's leading = scrollview's superview left,
... And content wrapper's trailing = scrollview's superview right.
When I ran the code, it shows everything in place perfectly; The UIScrollView scrolls, margins are properly set, etc.
However, Xcode throws an exception:
2015-02-05 18:06:58.230 ScrollViewApp[5353:180401] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7ff9fa49a3f0 H:[UIView:0x7ff9fa571560(600)]>",
"<NSLayoutConstraint:0x7ff9fa49b1a0 H:|-(0)-[UIView:0x7ff9fa571560] (Names: '|':UIScrollView:0x7ff9fa49a910 )>",
"<NSLayoutConstraint:0x7ff9fa49ce00 H:|-(0)-[UIScrollView:0x7ff9fa49a910] (Names: '|':UIView:0x7ff9fa49a840 )>",
"<NSLayoutConstraint:0x7ff9fa61c050 UIView:0x7ff9fa571560.right == UIView:0x7ff9fa49a840.trailing>",
"<NSLayoutConstraint:0x7ff9fa580970 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7ff9fa49a840(375)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7ff9fa49a3f0 H:[UIView:0x7ff9fa571560(600)]>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
After further googling, I found out that I can dismiss the warning by setting content wrapper's width (see step 5 above) priority to low; however it breaks the layout in interface builder. Take a look:
Compared to when it's set to High priority:
I know that in the end it makes no difference because it's both working as expected.. But I'm kind of curious as to why these things happen..
I'm trying to understand how UIScrollView works, but maybe I'm misunderstanding something; so, what is the proper way for setting up UIScrollView to work as expected?
Try to add a center horizontally constraint from your content wrapper to your scroll view.
To be honest, I really don't know why that works, i figured this one out by trial an error. If you want your view to be compatible different screen sizes, remove the width constraint of the content wrapper.
Hope it helped.
you can use scrollview like this. Add scrollview in design. Give it's left, top, right, bottom constraints in design. Add your subviews in scrollview. Add proper constraints for them. There is no need to add constraints in code. In viewDidLayoutSubviews set your scrollview's content size.
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
CGRect boundsOfSelf = self.view.bounds;
scrollView.frame = boundsOfSelf;
[scrollView setContentSize: boundsOfSelf.size];
}

Resources