NSLayoutConstraint errors only when switching view controllers - ios

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.

Related

Disabling Auto Layout For A Single UIImageView

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.

AutoLayout complains about constraints for 2 UITextFields with no borders

I am trying to build my login view that has only 2 TextFields, and 2 buttons. I am applying the "Add Missing Constraints" property so that Xcode applies the corresponding constraints to all the elements in my LoginViewController in the storyboard. I am restricting my app to run only in portrait mode, so I am using the "wCompact hRegular" setup.
Whenever I run my app and transition to my LoginViewController with a segue, my app displays only one textfield, and shows a bunch of errors in the console regarding autolayout.
The functionality I am trying to achieve is to come up with a login screen that resembles the new Parse login screen. That is having two textfields with no borders and only the bottom border of the username textfield showing so it can divide it from the password textfield, like so:
So far, this is how my app looks when it runs:
And it should look like this:
Here's the error that appears in the console:
2015-06-14 20:40:21.480 MyApp[71158:3600335] 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:0x7f9ff3f6d260 V:[_UILayoutGuide:0x7f9ff3f7a140(20)]>",
"<_UILayoutSupportConstraint:0x7f9ff3f087a0 V:|-(0)-[_UILayoutGuide:0x7f9ff3f7a140] (Names: '|':UIView:0x7f9ff3f7a030 )>",
"<_UILayoutSupportConstraint:0x7f9ff3f24730 V:[_UILayoutGuide:0x7f9ff3f7a2a0(0)]>",
"<_UILayoutSupportConstraint:0x7f9ff3f71640 _UILayoutGuide:0x7f9ff3f7a2a0.bottom == UIView:0x7f9ff3f7a030.bottom>",
"<NSLayoutConstraint:0x7f9ff3f6b9e0 V:[UITextField:0x7f9ff3f782a0]-(NSSpace(8))-[UITextField:0x7f9ff3f7a3d0]>",
"<NSLayoutConstraint:0x7f9ff3f58a40 V:[UIButton:0x7f9ff3f7ab00'Login']-(331)-[_UILayoutGuide:0x7f9ff3f7a2a0]>",
"<NSLayoutConstraint:0x7f9ff3f28b60 V:[_UILayoutGuide:0x7f9ff3f7a140]-(237)-[UITextField:0x7f9ff3f782a0]>",
"<NSLayoutConstraint:0x7f9ff3f34180 V:[UIButton:0x7f9ff3f7ad20'I forgot my password']-(49)-[UIButton:0x7f9ff3f7ab00'Login']>",
"<NSLayoutConstraint:0x7f9ff3f341d0 V:[UITextField:0x7f9ff3f7a3d0]-(35)-[UIButton:0x7f9ff3f7ad20'I forgot my password']>",
"<NSLayoutConstraint:0x7f9ff3f29f90 V:[UITextField:0x7f9ff3f782a0(30)]>",
"<NSLayoutConstraint:0x7f9ff3f055a0 V:[UIButton:0x7f9ff3f7ad20'I forgot my password'(30)]>",
"<NSLayoutConstraint:0x7f9ff3f055f0 V:[UIButton:0x7f9ff3f7ab00'Login'(30)]>",
"<NSLayoutConstraint:0x7f9ff3cc3b80 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7f9ff3f7a030(667)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7f9ff3f055f0 V:[UIButton:0x7f9ff3f7ab00'Login'(30)]>
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.
Does anyone know how to fix this and why it does not work? I even tried manually adding all the constraints manually, but it did not work. Also, if I give my textfields rounded borders and make them a little smaller than the storyboard's full width, it does what it's expected to do.
Thank you for you help in advance.
Cheers!
"Add Missing Constraints" is not always a good idea to add constraints..rather you should always prefer to add constraints manually...
Here is the image for your UI...I used wAnyhAny layout as it is good practice for add constraints for universal devices...
I used simply width constraint for textfield, rather you should Equal width to super view and add multiplier to resize width according to device width
Here is the output image in different sizes...

Unable to simultaneously satisfy constraints - Swift

Summary:
I just got finished doing auto-layout and when I run it, it is all scrunched together. Then I look at the command prompt and it says the error above. I've tried googling it but it just shows solutions for Obj-C and I'm doing it in Swift. How would I be able to solve this auto-layout issue?
Error:
2014-10-20 00:07:41.102 Fraction Calculator[74247:6698760] 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:0x7c440f10 V:[UIButton:0x7c440db0'Divide It!'(30)]>",
"<NSLayoutConstraint:0x7af7bdb0 V:[UIImageView:0x7af7bae0(8)]>",
"<NSLayoutConstraint:0x7ae928b0 V:[UITextField:0x7ae952d0(30)]>",
"<NSLayoutConstraint:0x7ae97110 V:[UITextField:0x7ae96f70(30)]>",
"<NSLayoutConstraint:0x7ae97eb0 V:[UILabel:0x7ae97d70'Enter Your Fraction To Be...'(48)]>",
"<NSLayoutConstraint:0x7ae99cd0 V:[_UILayoutGuide:0x7ae990c0]-(0)-[UILabel:0x7ae97d70'Enter Your Fraction To Be...']>",
"<NSLayoutConstraint:0x7ae9a000 V:[UILabel:0x7ae97d70'Enter Your Fraction To Be...']-(41)-[UITextField:0x7ae96f70]>",
"<NSLayoutConstraint:0x7ae9a090 V:[UITextField:0x7ae96f70]-(14)-[UIImageView:0x7af7bae0]>",
"<NSLayoutConstraint:0x7ae9a150 V:[UIImageView:0x7af7bae0]-(13)-[UITextField:0x7ae952d0]>",
"<NSLayoutConstraint:0x7ae9a270 V:[UITextField:0x7ae952d0]-(59)-[UIButton:0x7c440db0'Divide It!']>",
"<NSLayoutConstraint:0x7ae9a2a0 V:[UIButton:0x7c440db0'Divide It!']-(50)-[UILabel:0x7ae98df0]>",
"<NSLayoutConstraint:0x7ae9a300 V:[UILabel:0x7ae98df0]-(166)-[_UILayoutGuide:0x7ae99680]>",
"<_UILayoutSupportConstraint:0x7c441e80 V:[_UILayoutGuide:0x7ae990c0(20)]>",
"<_UILayoutSupportConstraint:0x7c441770 V:|-(0)-[_UILayoutGuide:0x7ae990c0] (Names: '|':UIView:0x7af7abc0 )>",
"<_UILayoutSupportConstraint:0x7c443610 V:[_UILayoutGuide:0x7ae99680(0)]>",
"<_UILayoutSupportConstraint:0x7c4429d0 _UILayoutGuide:0x7ae99680.bottom == UIView:0x7af7abc0.bottom>",
"<NSLayoutConstraint:0x7c44acf0 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7af7abc0(480)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7ae97eb0 V:[UILabel:0x7ae97d70'Enter Your Fraction To Be...'(48)]>
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.
An Image of My Constraints
https://www.dropbox.com/s/1l3ys9k7bdr8qrk/Screenshot%202014-10-20%2021.08.47.png?dl=0
An image of my iPhone 4 Preview
https://www.dropbox.com/s/7uoulzppalex2r8/Screenshot%202014-10-20%2021.09.48.png?dl=0
Image of the iPhone 4 in the Simulator
https://www.dropbox.com/s/c1qedgoytcnetri/iOS%20Simulator%20Screen%20Shot%20Oct%2020%2C%202014%2C%209.10.03%20PM.png?dl=0
If you guys have questions or need clarification please comment down below
It would be a bit hard to track which is the bad constraint by just looking at the error you have posted. But I can give a general rules or things to keep in mind while working with autolayout or constraints in general,
Prefer setting constraints in XIB/Storyboards. It will tell where exactly are you going wrong right when you are setting them. You can almost do everything in the interface builder unless its a special case.
Always set constraints in relative to views around it try not fix everything to super View
Use aspect ratio constraints whenever possible.
Try to use Intrinsic constraint sizes of views and avoid setting fixed widths/heights.
Remember you will always get into the kind of errors you are facing, when you set too many constraints. So always think are the constraints "just enough" to figure out its frame in run time. Not more or less but just enough.
Make use of in-equality constraints, they are very helpful. Also don't forget you have hugging Priorities and compression resistance priorities.
In your error, I see a lot of constraints which have fixed sizes, they would interfere when you have spacing constraint to those views. So start for the first, and keep the points I mentioned and delete the constraints you don't need.
References,
Auto layout Programming Guide
Guide for Debugging Auto layout issues
Objc issue
Raywenderlich Guide
Some Tips
I had a hard time figuring out what constraints were causing this error. Here is a simpler way to do it.
I'm using Xcode 6.1.1
"Command + A" to select all the UILabels, UIImages etc.
Click Editor -> Pin > (Select...) to Superview
again click Editor -> Resolve Auto Layout Issues -> Add Missing Constraints or Reset to Suggested Constraints. It depends on your case.

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.

Project really has no decent constraints yet, why is it giving me this constraint warning every time?

Every time I run my project, I get this warning in the console:
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) (
"<_UIScrollViewAutomaticContentSizeConstraint:0x8cc6830 UITableView:0xb3f5c00.contentHeight{id: 112} == -7.000000>" )
Will attempt to recover by breaking constraint
<_UIScrollViewAutomaticContentSizeConstraint:0x8cc6830
UITableView:0xb3f5c00.contentHeight{id: 112} == -7.000000>
Break on objc_exception_throw to catch this in the debugger. The
methods in the UIConstraintBasedLayoutDebugging category on UIView
listed in may also be helpful.
It's from a UITableView, and I'm definitely not touching the UIScrollView directly. It only has a label in each cell with these constraints:
I Googled and Googled but couldn't find anything. Does anyone know why it's complaining about UIScrollView?
if you want to remove the warnings, the quickest way to do it is in the storyboard.
select the view controller that causes the warnings to be displayed and select: clear all constrains in yourViewControllersName.
another option will be to select: reset to suggested constrains in yourViewControllersName.
these action will either clear all constraints, or add what ever Xcode thinks are the most suitable constraints to the view controller.
resting to suggested constraints will sometimes (or is it always?) add constraints that will not fit your needs..

Resources