Changing width constraint causes conflicts - ios

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.

Related

iOS - auto layout rotation not working as expected

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 ?

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.

I can't find a way to make Auto Layout constraints happy

I am trying to make this simple layout:
It's just a simple UICollectionViewCell.
But when I put all the auto layout constraints I believe are needed, I get this warning:
NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x17028dd40 H:[UIImageView:0x13fd3b000(48)]>",
"<NSLayoutConstraint:0x17028e2e0 H:|-(8)-[UILabel:0x13fd3bfe0'Something as a title...'] (Names: '|':_UIVisualEffectContentView:0x13fd3ace0 )>",
"<NSLayoutConstraint:0x17028e380 UILabel:0x13fd3bfe0'Something as a title...'.leading == UIImageView:0x13fd3b000.leading>",
"<NSLayoutConstraint:0x17028e470 H:[UIImageView:0x13fd3b000]-(8)-[UILabel:0x13fd3b140'Lady Oracle']>",
"<NSLayoutConstraint:0x17028e4c0 H:[UILabel:0x13fd3b140'Firstname Lastname']-(8)-| (Names: '|':_UIVisualEffectContentView:0x13fd3ace0 )>",
"<NSLayoutConstraint:0x17028e830 H:|-(0)-[UIVisualEffectView:0x13fe0ebd0] (Names: '|':UIView:0x13fe142d0 )>",
"<NSLayoutConstraint:0x17028e8d0 H:[UIVisualEffectView:0x13fe0ebd0]-(0)-| (Names: '|':UIView:0x13fe142d0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x17428ee70 h=-&- v=-&- _UIVisualEffectContentView:0x13fd3ace0.width == UIVisualEffectView:0x13fe0ebd0.width>",
"<NSAutoresizingMaskLayoutConstraint:0x17028fd20 h=--& v=--& H:[UIView:0x13fe142d0(50)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x17028dd40 H:[UIImageView:0x13fd3b000(48)]>
As can be inferred from the log I have the following constraints:
bottom container view: bottom, leading and trailing aligned to super view
title: top, leading and trailing spacing to container view. Bottom spacing to user avatar.
user avatar: to have specific size (width & height), horizontal aligned with the title, vertical spacing with the title, bottom space to container.
user name: top aligned to user avatar, leading spacing to user avatar, trailing spacing to container.
I can't seem to have sufficient constraints to make the layout how I want and get rid of the warning. The layout is appearing as expected, though.
There is two thing I can think of:
you forgot to set the views with..
[view setTranslatesAutoresizingMaskIntoConstraints:NO];
your collectionView cell size its to small when the collection view is trying to layout.

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