NSConstraint not working - ios

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!"

Related

UIImageView repeats showing in another cells

I dynamically add an UIImageView (as thumbnail) and its NSLayoutConstraints in my UIView which in the cell of a table. I have two problems about this.
After an image view added, if the user inserts text to this table still an image is being shown. I mean, instead of text table prints one more image. However, if I stop and re-run the application, this time text is shown as it should be. Still, if I write a text picture is coming. Why and what should I do?
I set image and its constraints like this:
-(void)setThumbnail:(UIImageView)imageView {
[self.messageView addSubview:imageView];
NSLayoutConstraint *leadingOfThumbnail = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.messageView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:8.f];
NSLayoutConstraint *trailingOfThumbnail = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.messageView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:-8.f];
NSLayoutConstraint *topOfThumbnail = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.messageView attribute:NSLayoutAttributeTop multiplier:1.0 constant:8.f];
NSLayoutConstraint *bottomOfThumbnail = [NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.messageView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-8.f];
[self.messageView addConstraint:leadingOfThumbnail];
[self.messageView addConstraint:trailingOfThumbnail];
[self.messageView addConstraint:topOfThumbnail];
[self.messageView addConstraint:bottomOfThumbnail];
[self.messageView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.messageView removeConstraints:[self.messageTextLabel constraints]];
}
And while loading, I get a constraint error. It says:
2016-10-07 09:35:32.532 application[3733:2922397] 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) (
"NSAutoresizingMaskLayoutConstraint:0x1281474d0 h=--& v=--& UIImageView:0x126d7e020.midY == + 100",
"NSAutoresizingMaskLayoutConstraint:0x128147520 h=--& v=--& V:[UIImageView:0x126d7e020(200)]",
"NSLayoutConstraint:0x126d6c880 V:|-(8)-[UIImageView:0x126d7e020] (Names: '|':UIView:0x1281480d0 )" )
Will attempt to recover by breaking constraint NSLayoutConstraint:0x126d6c880 V:|-(8)-[UIImageView:0x126d7e020] (Names: '|':UIView:0x1281480d0 )
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 checked, leading and top constrains set by me causes this error. Already they are not working correct. My image is 200 * 200, thus it does not need height and width constraint. I only want 8 pt leading, trailing, top, bottom from message view. What is the problem?
Thank you.
Explanation edit for first question: Table puts UIImageView randomly, multiple cells - not as it should be.
You may want to implement the prepareForReuse method and do some cleanup there, set the imageView to nil and remove it from the subview here.
This method gets called when your cell is dequeued and about to be reused in the tableview.
For the first issue, you can simply set cell image to nil after dequeing from tableview.
For the second issue, after ImageView initialization add this line, to avoid that autoresizingmask is translated to constraints:
imageView.translatesAutoresizingMaskIntoConstraints = false
I believe that your problem is because of the dequeCell functionality. UITbableView recycle cells. So if you place an image in one cell, there is a chance that it will appear in another cell.
To solve this, you need to place some conditions in your cellForRowAtIndexPath.
These conditions would check if you want text in your current cell or image or both.
If it only needs text, you need to remove the image from that cell.
If you still face problems, please share your cellForRowAtIndexPath code.

How to add Constraint programmatically in objective c

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];

Setting the NSLayoutAttributeLeading Space to Superview programmatically with some warnings

I am bringing an AdBannerView to the bottom of my app and I'm creating this programmatically because of the use of shared banners (rather than creating it in Storyboard). My app works in Portrait and Landscape mode and so in my viewWillAppear, I am setting the frame for the AdBannerView and I've also tried for the first time to work with AutoLayout programmatically but I'm having some issues.
I created the AdBannerView as a test in Storyboard and managed to apply the Leading Spaces to Superview, Trailing Spaces to Superview and Bottom Space to Superview constraints on the AdBannerView and so when I rotated the device, it would show up on the bottom in both Landscape and Portrait.
However, when doing the AutoLayout programatically, I'm not getting the same results.
Here's my code:
[self.adBanner setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-100, 320, 50)];
[self.view addSubview:self.adBanner];
NSLayoutConstraint *myConstraint =[NSLayoutConstraint
constraintWithItem:self.adBanner
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0];
[self.view addConstraint:myConstraint];
myConstraint =[NSLayoutConstraint constraintWithItem:self.adBanner
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1
constant:0];
[self.view addConstraint:myConstraint];
myConstraint =[NSLayoutConstraint constraintWithItem:self.adBanner
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBaseline
multiplier:1
constant:0];
[self.view addConstraint:myConstraint];
What I'm getting in the console is:
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:0x7fcb017020f0 ADBannerView:0x7fcb014c1860.bottom == UIView:0x7fcb014f9f70.lastBaseline>",
"<NSAutoresizingMaskLayoutConstraint:0x7fcb01624280 h=--& v=--& ADBannerView:0x7fcb014c1860.midY == + 957>",
"<NSAutoresizingMaskLayoutConstraint:0x7fcb016242f0 h=--& v=--& V:[ADBannerView:0x7fcb014c1860(66)]>",
"<NSLayoutConstraint:0x7fcb017039b0 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7fcb014f9f70(768)]>",
"<NSAutoresizingMaskLayoutConstraint:0x7fcb014fa8a0 h=--& v=--& 'UIView-Encapsulated-Layout-Top' V:|-(0)-[UIView:0x7fcb014f9f70] (Names: '|':UIViewControllerWrapperView:0x7fcb01619da0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fcb017020f0 ADBannerView:0x7fcb014c1860.bottom == UIView:0x7fcb014f9f70.lastBaseline>
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 device rotates but in Landscape mode, the AdBannerView is not shown, probably because it has been moved off-screen.
I've been through a number of tutorials on AutoLayout programatically and I honestly just don't get it.
Question:
Is there a way I can pin my AdBannerView to the left edge of the screen (Leading), to the right edge of the screen (Trailing) and to the Bottom of the screen (Bottom) programatically, so that when I rotate my iPhone/iPad, the AdBannerView shows there?
This app is iOS 7 and iOS 8 compatible.
First, you need to call [self.adBanner setTranslatesAutoresizingMasksIntoConstraints:NO]
Second, you will still need either a top offset constraint or a height constraint for the adBanner view.
Also, since you are using auto layout, you don't need to set the frame anymore, that will be done automatically by the layouting system.
Let me know if you need more help :)
You still need the height constraint. You can use something like:
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[view(==50)]" options:0 metrics:nil views:#{ #"view": self.adBanner }]];
You can call [self.view layoutIfNeeded] after you have setup all your constraints. Print out the frame for the adBanner, after the method call, for debugging purposes, to make sure all is how you want it.

Hide View and reposition other View with Autolayouts

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.

How to constrain a view to the bottom left of a UINavigationBar?

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.

Resources