iOS - auto layout rotation not working as expected - ios

I have app with enabled rotation for iPad (iPhone version is rotation-free). In my design, within storyboard, I have this hierarchy: UIView - UIScrollView - UIView. All "views" are set to have full width of the screen. Second UIView has class set to MyView, which is a class with an external xib file. Storyboard and xib are both created in a portrait mode.
Now for the problem.
If I run the app in the landscape mode, all is loaded OK. If I rotate
app after that, all is OK.
If I run the app in the portrait mode, the design "breaks". The entire content is thiner (so there is about 100+ px gap between the end of MyView and the end of the screen). If I rotate app, the design remains broken but correctly "enlarged". The gap is still of the same size. During the first launch, I got the following error in debugger:
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:0x1545b7800 h=-&- v=-&- UIView:0x1546a8cb0.width == ScrollableContentView:0x154575da0.width - 232>",
"<NSLayoutConstraint:0x15458dd80 H:[UIActivityIndicatorView:0x1546afa60]-(370)-| (Names: '|':UIView:0x1546ab840 )>",
"<NSLayoutConstraint:0x15458ddd0 H:|-(361)-[UIActivityIndicatorView:0x1546afa60] (Names: '|':UIView:0x1546ab840 )>",
"<NSLayoutConstraint:0x1546c7080 H:[UIView:0x1546ab840]-(0)-| (Names: '|':UIView:0x1546a8cb0 )>",
"<NSLayoutConstraint:0x1546c7120 H:|-(0)-[UIView:0x1546ab840] (Names: '|':UIView:0x1546a8cb0 )>",
"<NSLayoutConstraint:0x154579710 H:|-(0)-[ScrollableContentView:0x154575da0] (Names: '|':UIScrollView:0x15504c000 )>",
"<NSLayoutConstraint:0x154686ef0 UIScrollView:0x15504c000.centerX == ScrollableContentView:0x154575da0.centerX>",
"<NSLayoutConstraint:0x154697cc0 H:[UIScrollView:0x15504c000]-(0)-| (Names: '|':UIView:0x154547b80 )>",
"<NSLayoutConstraint:0x154697d10 H:|-(0)-[UIScrollView:0x15504c000] (Names: '|':UIView:0x154547b80 )>",
"<NSLayoutConstraint:0x15463b8c0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x154547b80(768)]>"
)
Will attempt to recover by breaking constraint
No matter what I do, I am unable to remove this problem. Even If I deleted almost every constraint I have still the same issue.

You should look if you don't have any auto layout warnings in your storyboard or xib file (yellow/red dot beside your controller name in document outline).
If you don't have any, do you set your constraints when adding your xib view ?
Have you think about content hugging priority or content compression resistance priority ?

Related

Constraints objective c

What does it mean
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:0x7f871d1303b0 H: [UIButton:0x7f871d197640(30)]>",
"<NSLayoutConstraint:0x7f871d1a1c40 H:[UIButton:0x7f871d196960'LB'(30)]>",
"<NSLayoutConstraint:0x7f871d1a4570 H:[UIView:0x7f871d196d60]-(100.8)-| (Names: '|':UIView:0x7f871ac66870 )>",
"<NSLayoutConstraint:0x7f871d1a5200 H:|-(0)-[UIView:0x7f871d196d60] (Names: '|':UIView:0x7f871ac66870 )>",
"<NSLayoutConstraint:0x7f871d19c5f0 H:[UIView:0x7f871ac66870]-(0)-| (Names: '|':UIView:0x7f871d199d70 )>",
"<NSLayoutConstraint:0x7f871d19c640 H:|-(20)-[UIView:0x7f871ac66870] (Names: '|':UIView:0x7f871d199d70 )>",
"<NSLayoutConstraint:0x7f871d1a8ca0 H:|-(0)-[UIView:0x7f871d199d70] (Names: '|':UIView:0x7f871d199c00 )>",
"<NSLayoutConstraint:0x7f871d1a8d40 UIView:0x7f871d19c820.width == UIView:0x7f871d199d70.width>",
"<NSLayoutConstraint:0x7f871d1a8de0 H:[UIView:0x7f871d199d70]-(10)-[UIView:0x7f871d19c820]>",
"<NSLayoutConstraint:0x7f871d1a8e80 H:[UIView:0x7f871d19c820]-(10)-[UIView:0x7f871d1a7000]>",
"<NSLayoutConstraint:0x7f871d1a8f20 UIView:0x7f871d1a7000.width == UIView:0x7f871d199d70.width>",
"<NSLayoutConstraint:0x7f871d1a8f70 H:[UIView:0x7f871d1a7000]-(0)-| (Names: '|':UIView:0x7f871d199c00 )>",
"<NSLayoutConstraint:0x7f871d1a9450 H:|-(15)-[UIButton:0x7f871d197640] (Names: '|':UIView:0x7f871d19b130 )>",
"<NSLayoutConstraint:0x7f871d1a9540 H:[UIButton:0x7f871d197640]-(10)-[UIView:0x7f871d199c00]>",
"<NSLayoutConstraint:0x7f871d1a95e0 H:[UIView:0x7f871d199c00]-(10)-[UIButton:0x7f871d196960'LB']>",
"<NSLayoutConstraint:0x7f871d1a96d0 H:[UIButton:0x7f871d196960'LB']-(10)-| (Names: '|':UIView:0x7f871d19b130 )>",
"<NSLayoutConstraint:0x7f871d1aa340 H:[UIView:0x7f871d19b130]-(0)-| (Names: '|':UIView:0x7f871d19afc0 )>",
"<NSLayoutConstraint:0x7f871d1aa390 H:|-(0)-[UIView:0x7f871d19b130] (Names: '|':UIView:0x7f871d19afc0 )>",
"<NSLayoutConstraint:0x7f871d1b7230 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7f871d19afc0(320)]>"
)
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.
That means you are giving more constraints than required so it's creates ambiguity.
For example if you have one label having height of 30.
Now you give constraints like top,leading and trailing with constant of 20 and fixed height of 30. that means your label always maintain space of 20 of each side from top,left and right and always keep height of 30.
That's fine.
now if you add one another constraint say fixed width with constant 50 that means label's width should be 50.
Now if your app running on 4 inch device then your screen width is 320.
So, if label maintain width of 50 then can't maintain left and right spacing of 20 and if it maintains spacing of 20 then can't maintain width.
This situation is called ambiguity.
This was an example for understanding.
Your constraints making this kind of ambiguity somewhere, so check twice and remove unnecessary or additional constraints.
hope this will help :)
Probably, something is wrong with constraints for UIView:0x7f871d199d70: as I see, you and/or XCode added offsets and also width.

Changing width constraint causes conflicts

I have an interface set up with constraints, with a menu bar, collection view, followed by a label, and finally a table view. I am trying to change the width of the collection view via an outlet to the width constraint. However, when I make this change, the collection view seems to move upward and I receive a conflict error. The error says there is a conflict between:
"<_UILayoutSupportConstraint:0x7ff61a519020 V:[_UILayoutGuide:0x7ff61a564eb0(64)]>",
"<_UILayoutSupportConstraint:0x7ff61a555a00 V:|-(0)-[_UILayoutGuide:0x7ff61a564eb0] (Names: '|':UIView:0x7ff61a5620c0 )>",
"<NSLayoutConstraint:0x7ff61a5665e0 V:[_UILayoutGuide:0x7ff61a564eb0]-(0)-[UICollectionView:0x7ff61b05ea00]>",
"<NSAutoresizingMaskLayoutConstraint:0x7ff61a4e1630 h=-&- v=-&- 'UIView-Encapsulated-Layout-Top' V:|-(0)-[UIView:0x7ff61a5620c0] (Names: '|':UIViewControllerWrapperView:0x7ff61a70c4e0 )>"
The constraint being broken is
<NSLayoutConstraint:0x7ff61a5665e0 V:[_UILayoutGuide:0x7ff61a564eb0]-(0)-[UICollectionView:0x7ff61b05ea00]>
What is causing this?
I figured out the reason for the error was I was attempting to call layoutIfNeeded within viewDidLoad.

Creating a "self-sizing" UITableViewHeaderView

OK, a really simple version of this that breaks...
Create a UIView in a nib.
Add an imageView to it. Give the image view a constraint for an Aspect Ratio of 1:1.
Now add constraints from the image view to the edge of the view. (0 size to pin it to the edges).
Now add the view as a UITableView tableHeaderView.
When you run the application on different devices it will break the constraints and remove the aspect ratio constraint. I don't want this, I want it to change the height of the view to be equal to the width.
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:0x7fad01efbbe0 UIImageView:0x7fad047209b0.width == UIImageView:0x7fad047209b0.height>",
"<NSLayoutConstraint:0x7fad04703630 H:[UIImageView:0x7fad047209b0]-(0)-| (Names: '|':VenueHeaderView:0x7fad047204c0 )>",
"<NSLayoutConstraint:0x7fad04703680 V:|-(0)-[UIImageView:0x7fad047209b0] (Names: '|':VenueHeaderView:0x7fad047204c0 )>",
"<NSLayoutConstraint:0x7fad04703740 V:[UIImageView:0x7fad047209b0]-(0)-| (Names: '|':VenueHeaderView:0x7fad047204c0 )>",
"<NSLayoutConstraint:0x7fad04703790 H:|-(0)-[UIImageView:0x7fad047209b0] (Names: '|':VenueHeaderView:0x7fad047204c0 )>",
"<NSLayoutConstraint:0x7fad0429bcf0 'UIView-Encapsulated-Layout-Height' V:[VenueHeaderView:0x7fad047204c0(300)]>",
"<NSLayoutConstraint:0x7fad0429bca0 'UIView-Encapsulated-Layout-Width' H:[VenueHeaderView:0x7fad047204c0(375)]>" )
Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7fad01efbbe0 UIImageView:0x7fad047209b0.width == UIImageView:0x7fad047209b0.height>
It seems that there are constraints UIView-Encapsulated-Layout-Height being added to the superview. But these are added with the wrong value. It should recalculate these based on the internal constraints of the header view.

AVPlayerViewController - No Auto Layout - Unable to simultaneously satisfy constraints.

In my project I have created a ViewController and created a class as "AVPlayerViewController" in order to show videos.
The code works and I am able to see the videos however, I get some constraints warnings in the console.
My project is working without AutoLayout and I use AutoResizing. I have tried the code:
self.view.translatesAutoresizingMaskIntoConstraints = false
but the warnings are not going away and the controls of the video player move to the top of the screen .
There warnings are:
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:0x1475c4540 h=-&- v=-&- _UIBackdropContentView:0x147443880.width == _UIBackdropView:0x147436a30.width>",
"<NSLayoutConstraint:0x1475b38f0 H:|-(0)-[_UIBackdropView:0x147436a30] (Names: '|':UIView:0x1474368d0 )>",
"<NSLayoutConstraint:0x1475b3980 H:[_UIBackdropView:0x147436a30]-(0)-| (Names: '|':UIView:0x1474368d0 )>",
"<NSLayoutConstraint:0x1475b3650 H:|-(0)-[UIView:0x1474368d0] (Names: '|':AVAlphaUpdatingView:0x14744db30 )>",
"<NSLayoutConstraint:0x1475b36d0 H:[UIView:0x1474368d0]-(0)-| (Names: '|':AVAlphaUpdatingView:0x14744db30 )>",
"<NSLayoutConstraint:0x1475c7670 H:|-(15)-[UIView:0x1474435c0](LTR) (Names: '|':_UIBackdropContentView:0x147443880 )>",
"<NSLayoutConstraint:0x1475c54d0 UIView:0x147443b40.right == _UIBackdropContentView:0x147443880.right>",
"<NSLayoutConstraint:0x1475c7a40 UIView:0x1474435c0.right == UIView:0x147443b40.left - 10>",
"<NSLayoutConstraint:0x1475c4da0 'UIView-Encapsulated-Layout-Width' H:[AVAlphaUpdatingView:0x14744db30(0)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1475b3980 H:[_UIBackdropView:0x147436a30]-(0)-| (Names: '|':UIView:0x1474368d0 )>
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.
Any Idea what to do?
As per this answer Unable to simultaneously satisfy constraints Warnings with AVPlayerViewController embedded in storyboard the solution seems to be to set showsPlayBackControls off on the PlayerController and then add it to the view.
Once you have an AVPlayer item, you can show the player controls again (assuming you need them).
I suspect that a valid AVPlayer item is needed in order for the player control constraints to be properly satisfied.
I had this exact same issue and managed to fix it by making this change. Good luck!
Strangely enough, what you do is create a second view under your main view, and then add your player view as a subview to that.
This very problem took me an entire day to solve a month ago, and that's how it got solved. You must remove any code that auto resizes constraints, though.
If you are programmatically creating the view for your view controller, add the AVPlayerViewController as a child view controller and its view as a subview in loadView() method instead of viewDidLoad(). No need to hide playback controls.

Autolayout Constraint Error

I have a problem with autolayout contraints on my iPad project, when I rotate my device I have this error :
2013-06-03 13:16:24.969 PDFViewer[7534:907] 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:0x1f5525e0 H:[UIScrollView:0x1f551910]-(970)-| (Names: '|':UIView:0x1f547740 )>",
"<NSLayoutConstraint:0x1f5525a0 H:|-(0)-[UIScrollView:0x1f551910] (Names: '|':UIView:0x1f547740 )>",
"<NSAutoresizingMaskLayoutConstraint:0x1f562520 h=--- v=--- H:[UIWindow:0x1f543890(768)]>",
"<NSAutoresizingMaskLayoutConstraint:0x1f560af0 h=-&- v=-&- UIView:0x1f547740.width == UIWindow:0x1f543890.width>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x1f5525e0 H:[UIScrollView:0x1f551910]-(970)-| (Names: '|':UIView:0x1f547740 )>
Ok, some constraints are contentious but I don't set any constraint by myself, they are all set automatically in Interface Builder and I can't remove them. What I want is pretty simple, a scrollview, with fixed width and 100% height, and a collection view, with remaining width and 100% height.
Autolayout makes me crazy, any idea ?
Edit : Here are my constraints defined in IB
Error was caused by an automatic constraint, more precisely this one :
"<NSLayoutConstraint:0x1f5525e0 H:[UIScrollView:0x1f551910]-(970)-| (Names: '|':UIView:0x1f547740 )>",
As it is added by IB you can't delete it, so for ignoring it I have set the priority to 1.

Resources