Say, in the xib file I add a UIView and its width and height is fixed. No LayoutConstraint added in the xib file.I connect the UIView with an IBOutlet property "contentView", then in the viewDidLoad method I do :
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
contentView.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0f constant:-20.0f];
[self.view addConstraint:constraint];
}
When I ran the program , it seems that constraint conflict happened, but I don not know how to solve this problem:
2014-10-14 09:37:57.436 TestAutoLayout[651:53850] 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)
(
"<NSIBPrototypingLayoutConstraint:0x7a028de0 'IB auto generated at build time for view with fixed frame' H:|-(0)-[UIView:0x7a029380](LTR) (Names: '|':UIView:0x7a029470 )>",
"<NSIBPrototypingLayoutConstraint:0x7a028ed0 'IB auto generated at build time for view with fixed frame' H:[UIView:0x7a029380(320)]>",
"<NSLayoutConstraint:0x7a028620 UIView:0x7a029380.right == UIView:0x7a029470.right - 20>",
"<NSLayoutConstraint:0x7a02f600 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7a029470(320)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7a028620 UIView:0x7a029380.right == UIView:0x7a029470.right - 20>
For some reason I have to set the constraint in program. Thanks.
Related
I want to add constraint programmatically and I use below code to add TOP and LEFT constraint.
NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:label1
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1
constant:110];
NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:label1
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1
constant:10];
lable1 is added in my view controller. When I add this two constraint in the view like
[self.view addConstraint:top];
[self.view addConstraint:left];
It gives the error in the consol and constraint does not affect the lable.
2016-02-09 19:36:59.824 testinMRC[99160:313382] 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:0x7fa5c8738610 V:|-(110)-[UILabel:0x7fa5c8626940'Label'] (Names: '|':UIView:0x7fa5c86267b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fa5c8628a70 'IB auto generated at build time for view with fixed frame' V:|-(88)-[UILabel:0x7fa5c8626940'Label'] (Names: '|':UIView:0x7fa5c86267b0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fa5c8738610 V:|-(110)-[UILabel:0x7fa5c8626940'Label'] (Names: '|':UIView:0x7fa5c86267b0 )>
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-02-09 19:36:59.889 testinMRC[99160:313382] 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:0x7fa5c87334b0 H:|-(10)-[UILabel:0x7fa5c8626940'Label'] (Names: '|':UIView:0x7fa5c86267b0 )>",
"<NSIBPrototypingLayoutConstraint:0x7fa5c86285c0 'IB auto generated at build time for view with fixed frame' H:|-(188)-[UILabel:0x7fa5c8626940'Label'](LTR) (Names: '|':UIView:0x7fa5c86267b0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fa5c87334b0 H:|-(10)-[UILabel:0x7fa5c8626940'Label'] (Names: '|':UIView:0x7fa5c86267b0 )>
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.
Can anyone tell me why this error come?
Help me to short out the solution
In your case error clearly states that you have conflicting constraints for your view. Probably because you a trying to add constraints to the view that already has some from Interface Builder. Even if you didn't set up any constrains explicitly IB does provide them for you when it detects that some are missing.
I see in comments you've mentioned that you want to do everything programmatically.In that case take a look at piece of code:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = [UIView new];
view.backgroundColor = [UIColor redColor];
[view setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:view];
NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:100];
NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:100];
NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:50];
NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:50];
[self.view addConstraints:#[left, top]];
[view addConstraints:#[height, width]];
}
It's pretty self-explanatory. As you can see I had to add width and height constraints to the view, because only left and top doesn't fully describe it's position.
Result:
I encourage you to try out Visual Format Language. Same result can be achieved with much less code. This code leads to the very same result:
NSArray *horizontal = [NSLayoutConstraint constraintsWithVisualFormat:#"|-100-[view(50)]" options:0 metrics:nil views:#{#"view" : view}];
NSArray *vertical = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-100-[view(50)]" options:0 metrics:nil views:#{#"view" : view}];
[self.view addConstraints:vertical];
[self.view addConstraints:horizontal];
Let me know if this helped. Cheers.
The problem is not your code, it is the IB auto generated at build time for view with fixed frame message:
Interface Builder automatically generates constraints for you if you don't add them yourself.
You could avoid it by either
Select the nib or storyboard in the Project Navigator, go to the File Inspector and uncheck Use Auto Layout.
or
Add some constraints in Interface Builder and set them to remove at build time.
It looks like you have conflicting layout constraints. A constraint was most likely set up in storyboard and you are trying to add a constraint programmatically that directly conflicts with it.
It looks like you are trying to move a UIView out of the way after an action occurs. I.E press a button and move a happy face picture from the middle of the screen to the top left corner.
Adding and removing constraints directly in code can be a HUGE headache with code bloat, unit testing and warnings filling up your console.
It might be a better idea to create the main UIView in storyboard and have a hidden placeholder UIView that holds all the constraints you want the UIView to have after you press the button. Then you can switch out all the constraints programmatically for those UIViews. So the view you want to modify will have the position, width and height of the placeholder and the place holder will have the position and height of the view you want to modify.
This way you will never have this error while changing the layout constraints programmatically because any errors will be caught by the storyboard.
There's some open source that does this for you in one or two lines of code. (There's some youtube video tutorials in the docs too) https://cocoapods.org/pods/SBP
After installing the cocoapod, your code would look something like this
this would be code inside the view controller with the UIView you want to modify and the placeholder UIView
//UIView* viewBeforeChange
//UIView* hiddenViewAfterChange
[self switchViewConst:viewBeforeChange secondView:hiddenViewAfterChange];
I have a toolbar with a UIImageView I added programmatically. When I tried adding NSConstraint to the UIImageView to center it in the toolbar view. When I ran it on the simulator, it crashed.
Here's my code:
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,
nav.frame.size.width, nav.frame.size.height)];
image.image = [UIImage imageNamed:[imageTop objectAtIndex:2]];
[imageToolbar addSubview:image];
[imageToolbar addConstraint:[NSLayoutConstraint constraintWithItem:image
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:imageToolbar
attribute:NSLayoutAttributeCenterX
multiplier:1.0 constant:0]];
[imageToolbar addConstraint:[NSLayoutConstraint constraintWithItem:image
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:imageToolbar
attribute:NSLayoutAttributeCenterY
multiplier:1.0 constant:0]];
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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7fcfc2536f50 UIImageView:0x7fcfc2536d50.centerX == UIToolbar:0x7fcfc2525610.centerX>",
"<NSLayoutConstraint:0x7fcfc252dad0 UIView:0x7fcfc252c750.trailingMargin == UIToolbar:0x7fcfc2525610.trailing - 16>",
"<NSLayoutConstraint:0x7fcfc252db20 UIToolbar:0x7fcfc2525610.leading == UIView:0x7fcfc252c750.leadingMargin - 16>",
"<NSAutoresizingMaskLayoutConstraint:0x7fcfc254fea0 h=--& v=--& UIImageView:0x7fcfc2536d50.midX == + 160>",
"<NSLayoutConstraint:0x7fcfc2551110 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fcfc252c750(375)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fcfc2536f50 UIImageView:0x7fcfc2536d50.centerX == UIToolbar:0x7fcfc2525610.centerX>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.
Thanks.
You have created an UIImageView but you have not added that image view to the interface. You cannot add constraints for a view that is not in the interface. If you do, you will, uh, crash.
Also, you have neglected to set translatesAutoresizingMaskIntoConstraints to NO. If you neglect to do that, then when you add your own constraints, you will have a constraint conflict and you will, uh, crash.
Really, it's just a matter of reading the error message in the console. As my old folk dancing teacher used to say, "The music tells you what to do!"
I added 1 view in XIB, it is set autolayout. But when run, i want to to change frame of that view. I know if using autolayout, need to set translatesAutoresizingMaskIntoConstraints = YES in my code. I have done that, I create new frame for that view but it shows the warning message in console:
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)
(
"<NSIBPrototypingLayoutConstraint:0x16e4b750 'IB auto generated at build time for view with fixed frame' V:|-(0)-[UIView:0x16ed3c30] (Names: '|':UIView:0x16ee06b0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x16e19e30 h=-&- v=-&- UIView:0x16ed3c30.midY == UIView:0x16ee06b0.midY + 44>",
"<NSAutoresizingMaskLayoutConstraint:0x16e19e60 h=-&- v=-&- UIView:0x16ed3c30.height == UIView:0x16ee06b0.height>"
)
Will attempt to recover by breaking constraint
<NSIBPrototypingLayoutConstraint:0x16e4b750 'IB auto generated at build time for view with fixed frame' V:|-(0)-[UIView:0x16ed3c30] (Names: '|':UIView:0x16ee06b0 )>
Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
This is the code for create new frame, i just want the origin of Y is equal to 0.
self.leftView.frame= CGRectMake(0, 0, 40, self.view.frame.size.height);
How can i fix that bug? Please give some advice. Thanks much.
If I understand you correctly, I think you need to use a stored NSLayoutConstraint variable in your class, then update the constraint's constant value to 0 to make your UIView slide to the top.
Also, when you're using autolayout, you usually set:
myView.translateAutoResizingMaskIntoConstraint = NO;
That's NO instead of YES.
So an example would be something like:
#interface MyViewController()
{
NSLayoutConstraint *myViewTopConstraint;
}
#end
#implementation MyViewController
-(void)setupMyConstraintsMethod
{
...
// ---------------------------------------------------------------
// This constraint here says "myView" top edge should be pinned
// to the top edge of the view controller's view but with an offset
// of 50 pixels.
// ---------------------------------------------------------------
NSLayoutConstraint *myViewTopConstraint = [NSLayoutConstraint constraintWithItem:self.myView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:50.0];
[self.view addConstraint:myViewTopConstraint];
}
-(void)buttonPressed:(id)sender
{
// ------------------------------------------------------------
// now you want myView's top edge to be at the top of your
// view controller's view, so you set the constant to 0
// ------------------------------------------------------------
myViewTopConstraint.constant = 0;
// animates the sliding up of "myView"
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];
}
#end
So guys,
I have a profile view that contains several informations. Some of them don't have to be set e.g. Biografie. So depending if its set, I would like to hide the Biografie View (checkout screenshot) and reposition the UserData View and the ActivityData View AND change the Size of their superview to fill the space made by the Biografie View.
Here is my actual structure of my TableViewHeader:
The other point that i have to care about is that, the UILabel have to be mutliline and fit its content.
So my questions:
How do I reposition the UserData & ActivityData View to fill the
space made by the hidden BiografieView?
How do I make the Biografie
View height fit the UILabels text?
Please keep in mind that I'm using Autolayout. So I have to use constraints to modify the positions right?
To be honest.. Basically it should be like Instagram, when adding a Biografie. I hope I made it clear what I want..
Edit 1
I have tried your suggestion Damien, but nothing is changing:
NSLog(#"Height %f, Width %f", self.labelBiografie.frame.size.height, self.labelBiografie.frame.size.width);
if ([self.profileUser objectForKey:kGSUserBiografieKey]) {
self.labelBiografie.text = [self.profileUser objectForKey:kGSUserBiografieKey];
[self.labelBiografie sizeToFit];
}else{
[self.labelBiografie setHidden:YES];
[self.countContainer setConstraintConstant:-self.labelBiografie.frame.size.height forAttribute:NSLayoutAttributeCenterY];
[self.tableHeaderViewChildVIew setConstraintConstant:-self.labelBiografie.frame.size.height forAttribute:NSLayoutAttributeHeight];
self.tableView.tableHeaderView = self.tableHeaderViewChildVIew;
}
EDIT 2:
Using this Project (UIView-UpdateAutoLayoutConstraints
) I'm getting following error on the methods:
Line 56: [self hideView:hidden byAttribute:NSLayoutAttributeHeight];
Line 85: [self setConstraintConstant:0 forAttribute:attribute];
Line 23:
[self.superview addConstraint: [NSLayoutConstraint constraintWithItem:self attribute:attribute relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:constant]];
ERROR:
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:0x8c1b8e0 V:|-(0)-[UIButton:0x8c6fb40] (Names: '|':UIView:0x8c6f630 )>",
"<NSLayoutConstraint:0x8c19dc0 V:[UIButton:0x8c6fb40(100)]>",
"<NSLayoutConstraint:0x8c1d740 V:[UIButton:0x8c6fb40]-(NSSpace(8))-[UILabel:0x8c70900]>",
"<NSLayoutConstraint:0x8c20df0 V:[UILabel:0x8c70900]-(NSSpace(8))-[UIView:0x8c16a00]>",
"<NSLayoutConstraint:0x8c20fa0 V:[UIView:0x8c16a00(40)]>",
"<NSLayoutConstraint:0x8c23790 V:[UIView:0x8c16a00]-(0)-| (Names: '|':UIView:0x8c6f630 )>",
"<NSAutoresizingMaskLayoutConstraint:0x8c78e60 h=--& v=--& V:[UIView:0x8c6f630(217)]>",
"<NSLayoutConstraint:0x8c5d000 V:[UILabel:0x8c70900(0)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x8c20df0 V:[UILabel:0x8c70900]-(NSSpace(8))-[UIView:0x8c16a00]>
Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
This error shows up on the Demo-Project out of the box, on my on Project it also get returned when i hide the view..
I built a category to answer to this problem I also already encountered :
Hide autolayout UIView : How to get existing NSLayoutConstraint to update this one
EDIT:
If you use autolayout, do not use view.frame any more and don't forget to set all you autolayout view like this:
myView.translatesAutoresizingMaskIntoConstraints = NO;
Using this category https://github.com/damienromito/UIView-UpdateAutoLayoutConstraints
if(!user.biografie && user.biografie.lenght == 0)
{
[biografieLabel setConstraintConstant:0 forAttribute:NSLayoutAttributeHeight];
}else
{
biografieLabel.text = user.biografie;
}
For your question two , the first solution would be to had a height constraint between the uilabel and its container's height(BiografieView) .
NSLayoutConstraint *constraint = [NSLayoutConstraint
constraintWithItem:biografieView /// change here
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:biografieLabel /// change here
attribute:NSLayoutAttributeHeight
multiplier:1
constant:0];
[view addConstraint:constraint];
Don't forget to set uilabel.numberOfLine = 0 ,
the height of this one will be automatically updated
BUT, BUT, BUT...* (better solution for your case)
If you have only one uilabel in your biografie view, remove this container. The uilabel is also a uiview, and you will not have this problem any more.
My goal is to add a logo to the bottom left corner of a UINavigationBar, and to add constraints so that it stays there upon rotation.
Here's what I've tried:
UIView* navBarView = [[self navigationController] navigationBar];
UIImageView* logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"asdf"]];
[navBarView addSubview:logoImageView];
NSLayoutConstraint *logoConstraintLeftAlign =
[NSLayoutConstraint constraintWithItem:logoImageView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:navBarView
attribute:NSLayoutAttributeLeft
multiplier:1.0f
constant:0.0f];
NSLayoutConstraint *logoConstraintBottomAlign =
[NSLayoutConstraint constraintWithItem:logoImageView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:navBarView
attribute:NSLayoutAttributeBottom
multiplier:1.0f
constant:0.0f];
[navBarView addConstraint:logoConstraintLeftAlign];
[navBarView addConstraint:logoConstraintBottomAlign];
But that produces errors regarding some conflicting constraints:
2013-10-17 13:34:07.202 WTTest6[6551:a0b] 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:0x13196350 UIImageView:0x13198280.bottom == UINavigationBar:0x9d98970.bottom>",
"<NSAutoresizingMaskLayoutConstraint:0x1318a6c0 h=--& v=--& UIImageView:0x13198280.midY == + 12>",
"<NSAutoresizingMaskLayoutConstraint:0x1318a6f0 h=--& v=--& V:[UIImageView:0x13198280(24)]>",
"<NSAutoresizingMaskLayoutConstraint:0x131aa0e0 h=-&- v=--& V:[UINavigationBar:0x9d98970(44)]>"
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x13196350 UIImageView:0x13198280.bottom == UINavigationBar:0x9d98970.bottom>
I looked at the refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints, but was a bit overwhelmed by it, as I am just working with constraints for the first time.
I tried setting the frame of the logoImageView, which worked for the default orientation. I suppose I'd have to change the frame each time the orientation changes. But that seems contrary to the point of using AutoLayout and constraints.
So my question is, Can I use constraints to anchor the view like I want, and if so, how do I avoid the NSAutoresizingMaskLayoutConstraint "problem"?
Note that I looked at Putting a custom UIView at the bottom of a UINavigationBar but that suggested using the titleView of the UINavigationBarItem, which is centered, and thus does not help with the left alignment.
Well first off, what I found is that basically ANY time I add a constraint to something and I get conflicts, it's because I did NOT add this to the object I am adding the constraint to:
[someUIView setTranslatesAutoresizingMaskIntoConstraints:NO];
DO NOT put this on the container view, just the subViews.
Set 'autoresizingMask' on 'navigationBar' subviews for your layout.
keep setTranslatesAutoresizingMaskIntoConstraints = YES on all subviews.
It works for me.