AutoLayout complains about constraints for 2 UITextFields with no borders - ios

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...

Related

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.

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.

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.

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