Disabling Auto Layout For A Single UIImageView - ios

I have a UIImageView in my photo app that changes size based upon the imported image. The size is created dynamically and then positioned programmatically in the center of the screen without using auto layout.
Now to do this I have used
self.imageViewCanvas.translatesAutoresizingMaskIntoConstraints = YES;
[self.imageViewCanvas setFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
to disable the constraints. The problem is that whenever I load the app I get a long warning message that contains the following:
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)
(
"",
""
)
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 may also be helpful.
Is there a way to disable this particular warning or can I adjust the code in some way to avoid it?
Thanks

For a storyboard or a NIB with auto layout enabled, Xcode will provide constraints that are missing. This happens at build time. If you look at the Size inspector for a view to which you haven't added any constraints, you'll see a message about Xcode adding constraints for you. So, basically, no matter what you do, the image view will be constrained.
If you then set translatesAutoresizingMaskIntoConstraints to true, that will conflict with the automatically-supplied constraints. You could remove/deactivate those constraints, but it's hard to obtain references to them.
Your options:
Use auto layout to position the image view. It's easy to accomplish what you want.
Turn off auto layout for the whole NIB, so there will be no constraints and translatesAutoresizingMaskIntoConstraints will be true by default.
Add sufficient constraints of your own in the NIB but mark them to be removed at build time on the Attributes or Size inspector. This signals to Xcode that you want to take over and prevents it from supplying its own constraints. (This is normally done when you will be supplying constraints programmatically, but it can also work if you're just going to turn on translatesAutoresizingMaskIntoConstraints.)
Add constraints of your own and set up outlets to them. Deactivate them programmatically before turning on translatesAutoresizingMaskIntoConstraints.

Related

Adding a button in dynamic TableViewCell

I have a label of dynamic size in a TableViewCell. I have pinned it to up,down,left and right and set Lines to 0 and it is working fine. Next, I add a button below the label and and pin it to top and left. and fix the width and height. But I am getting an error 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:0x7fcbc302f600 UILabel:0x7fcbc3034010'Digestion of food in the...'.top == UITableViewCellContentView:0x7fcbc30334a0.topMargin>",
"<NSLayoutConstraint:0x7fcbc3037e30 UITableViewCellContentView:0x7fcbc30334a0.bottomMargin == UILabel:0x7fcbc3034010'Digestion of food in the...'.bottom + 38>",
"<NSLayoutConstraint:0x7fcbc303a920 'UIView-Encapsulated-Layout-Height' V: [UITableViewCellContentView:0x7fcbc30334a0(43.5)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fcbc3037e30
UITableViewCellContentView:0x7fcbc30334a0.bottomMargin ==
UILabel:0x7fcbc3034010'Digestion of food in the...'.bottom + 38>
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. Here is the screenshot of my layout
Which constraint should I remove? I don't think I have added extra constraints
In interface builder check that there are no warnings (the yellow arrow on the side of the vc inspector) and check that the height of the label +the button and the margins don't exceed the cell height.
Also you said you pinned to the top, of what? The container or the label?
Use these constraint for as per your requirements:
OR
Follow these links Autolayout or Autolayout programatically
(OR)
For some reason, Xcode generates own default set of auto layout constraints on a NIB at build time. This is why I couldn't add any more manual constraints, because they were conflicting with the automatically added ones.
I resolved this the following way:
Open up the Storyboard view controller you're handling.
Select the view controller and select Editor > Resolve Auto Layout Issues > All Views in [ ] View Controller > Add Missing Constraints from the menu:
This will ensure that no additional build time constraints are created and all the constraints are now visible.-->Select all the constraints from your view controller:
This will ensure that no additional build time constraints are created and all the constraints are now visible.-->Check from the right pane the following check box: Placeholder - Remove at build time:
(This will ensure that no additional build time constraints are created and all the constraints are now visible.)
Now you can add all the auto layout constraints manually in the code.
you will have to provide some more info than that regarding the values you set in auto layout like the distance of the UILabel from top margin,down,left etc.
The problem might be that you specified the UIButton to be from top margin , it should be with respect to your UILabel as its below it so set the UIButton's vertical spacing property to the UILabel this makes sure the UIButton is placed below it.

NSLayoutConstraint errors only when switching view controllers

I'm getting some very weird behaviour from one of my view controllers.
I'm navigating to the same controller (with different instances of it) from two different UITableViews in different tabs.
When it gets displayed from a tap on a UITableViewCell, everything is fine and no errors display. If I have the controller open and I navigate away from it by selecting another tab, then navigating back to it through pressing the original tab, I get a large amount of NSLayoutConstraint errors.
I don't actually have any constraints in my cell and I don't have any UI elements on the cell's content view in the Storyboard, as I'm adding all of them programmatically. I'm not defining any constraints in the code either.
The full error is here, as it's too large to reasonably paste into SO.
An excerpt is as follows:
2015-04-11 16:49:23.889 TradingPost[6982:60b] 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:0x155e5250 H:[UILabel:0x155d8150(158)]>",
"<NSLayoutConstraint:0x155ed9e0 H:|-(8)-[UILabel:0x155d8150] (Names: '|':UITableViewCellContentView:0x155d4a80 )>",
"<NSLayoutConstraint:0x155eda30 H:[UILabel:0x155d8150]-(47)-[UIView:0x155e01a0]>",
"<NSLayoutConstraint:0x155f1bc0 UIView:0x155e01a0.trailing == UITableViewCellContentView:0x155d4a80.trailing - 8>",
"<NSAutoresizingMaskLayoutConstraint:0x156b7640 h=--& v=--& H:[UITableViewCellContentView:0x155d4a80(0)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x155e5250 H:[UILabel:0x155d8150(158)]>
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.
Addition: Upon further investigation, I find I'm still getting errors, even when I remove all UI generating code from the controller and all constraints from the Storyboard. At this point I'm suspecting witchcraft.
Even if you don't create any constraints explicitly, the auto layout system can create them from the autoresizingMask mask associated with each view. You can try setting translatesAutoresizingMaskIntoConstraints to NO. However, if you're using auto layout and size classes to create an interface that adapts to various screen sizes, you'll probably need to embrace constraints. In that case, you might be better off setting the relevant constraints explicitly in your code.
I found that another view, attached to the one that I thought was causing the trouble, was the actual cause of the problem.
All it needed was a reset of the constraints to fix it, I just completely forgot about it when I encountered this problem.

Using auto layout and systemLayoutSizeFittingSize

I have a really simple view that I cannot for the life of me figure out the problem. I have successfully used auto layout and systemLayoutSizeFittingSize for fairly complex custom UITableViewCells. This is my first time using it for a UIView (to be used in a UITableView.tableHeaderView) and I cannot get it to work.
I have a simple label in the view that I want to cover the entire view. I have added the Trailing, Leading, Top, and Bottom constraints to the label against the parent's corresponding edges. When I do this, systemLayoutSizeFittingSize computes the correct height, but 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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7fbb71d9a310 V:|-(5)-[UILabel:0x7fbb71df5120'Test Label'] (Names: '|':mailapp.MailTableHeaderView:0x7fbb71d2c490 )>",
"<NSLayoutConstraint:0x7fbb71d2ebf0 UILabel:0x7fbb71df5120'Test Label'.bottom == app.HeaderView:0x7fbb71d2c490.bottom - 5>",
"<NSLayoutConstraint:0x7fbb71de31d0 V:[app.HeaderView:0x7fbb71d2c490(0)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fbb71d2ebf0 UILabel:0x7fbb71df5120'Test Label'.bottom == app.HeaderView:0x7fbb71d2c490.bottom - 5>
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.
Now, if I remove the "bottom" constraint on the label, auto layout works correctly but now systemLayoutSizeFittingSize returns 0.
What is the right way to make the constraints satisfy the auto layout engine but also to have systemLayoutSizeFittingSize compute the correct size?
As I mentioned, the most baffling thing is that I have used this successfully for complex auto layouts, but I noticed all those were UITableViewCells. I'm not sure if there is a difference.
I figured out the problem, but I don't understand why. For the UITableViewHeader, I cannot do the typical auto layout thing for the view. That is, I have to set translatesAutoresizingMaskIntoConstraints to be true. Effectively, I cannot use auto layout for the header view, and I have to set the frame manually.
Within the header view, I can use auto layout for the sub views successfully.
So my algorithm goes like this for constructing the tableHeaderView:
Create the header view with any arbitrary frame
Compute the constraints (of the header view's subviews)
Set the frame.width of the header view to match the parent tableView.frame.width
Layout the header view
Use systemLayoutSizeFittingSize to compute the height
Update the header view's frame.height
This works for me. I was not able to get it to work if I created the header view with translatesAutoresizingMaskIntoConstraints set to false.
As per the Apple Visual Format Language the following appears to be setting a '0' height constraint on your HeaderView.
E.g.
"<NSLayoutConstraint:0x7fbb71de31d0 V:[app.HeaderView:0x7fbb71d2c490(0)]>"
I would start by investigating this, as having a height of 0 will likely mess up your other label constraints for top and bottom spacing.

Can't get constraints right in iOS8 custom keyboard

I'm having major problems with auto layout in iOS 8. I'm working on a custom keyboard, but I can not get the view to display at the correct height. The rendered view on the device comes out too low, more like the intended height of the UIImageView alone. I've set the constraints as shown in the image:
But I keep getting this error in the log, that tells me nothing (probably because I don't understand where to look):
2014-12-06 19:56:45.095 [40268:12181198] 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:0x600000096940 V:[UIInputView:0x7fe780d3f600(246)]>",
"<NSLayoutConstraint:0x600000097390 'UIView-Encapsulated-Layout-Height' V:[UIInputView:0x7fe780d3f600(0)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600000096940 V:[UIInputView:0x7fe780d3f600(246)]>
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've tried following the code in iOS 8 Custom Keyboard: Changing the Height, but it still wont work.
Edit:
I'm setting my height constraint using the following function:
override func updateViewConstraints() {
super.updateViewConstraints()
// Add custom view sizing constraints here
let changeToOrientation = deviceInfoService.getOrientationForView(self.inputView)
if (changeToOrientation == Orientation.Portrait) {
self._heightConstraint!.constant = 246
self.inputView.addConstraint(self._heightConstraint!)
} else {
self._heightConstraint!.constant = 192
self.inputView.addConstraint(self._heightConstraint!)
}
}
This looks like a bug in the iOS Custom Keyboard implementation. The workaround currently is to adjust your constraint's priority to more/less than 1000
The error tells you that you have conflicting constraints. UIView-Encapsulated-Layout-Height seems to be a pre-defined height (When searching I mostly found references to its usage in UICollectionView). My guess is that it's the height defined by the OS that your keyboard is allowed to be. The other constraint is most likely the intrinsic content size of your created view (316 and 30 does indeed add up to 246).
My conclusion is that you probably did something wrong when you changed the height. As I'm not too familiar with keyboard extension development I can't tell you what. However when it comes to the Today Extensions which I have worked a bit with, there was a method you could override which specified the height of the widget.
This looks similar to how a TodayExtension height can be provided. Try this:
self.preferredContentSize = CGSizeMake(320, 246);
That's how I set the height of my today extension vs setting height constraints explicitly. I'll try this myself, just putting together a sample keyboard project now.

Auto layout constraints in code conflicting with sizes in XIB

I've set up a number of UILabels and UIButtons within 3 subviews in a storyboard. In code, I've defined auto layout constraints for all of these however for some reason when I run the app, the sizes that I have defined in the storyboard are conflicting with the constraints in code.
For example, one subview is positioned at 0,0 in the XIB with height 200 and width 320, simply for me to layout the elements before writing the code. There are no constraints in the storyboard.
This subview has a number of UILabels and UIButtons within it and their cumulative height is supposed to define the height of the subview. This should end up at 205pts height, however the log shows a conflict:
2014-06-02 16:45:38.506 discounter[11691:60b] 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:0x109390160 'IB auto generated at build time for view with fixed frame' V:[UIView:0x109389010(200)]>",
"<NSLayoutConstraint:0x109249510 V:[UIView:0x109389010(205)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x109249510 V:[UIView:0x109389010(205)]>
I have set all my views to have translatesAutoresizingMaskIntoConstraints = NO so I'm at a loss as to why this is happening. It appears to be happening to a number of other elements too, but I have a feeling its probably the same reason why.
Can anyone help?
Thanks!
Add those constraints that you will replace in your code in your storyboard, and check their "remove at build time" properties. Like this:
Background:
This is a way for you to promise Xcode that you will add the constraint in code, and thus will prevent Xcode from auto generating the necessary constraints. The auto generation is necessary, as otherwise the runtime wouldn't be able to determine how to present the view in question. Generally, you should strive to define all your constraints in the storyboard. You could also IBOutlet a constraint to your code, and then edit its constant value when the app is ran, that way avoiding the tedious adding of constraints in your code.

Resources