Re: NSLayoutConstraint crash
Re: NSLayoutConstraint crash
- Subject: Re: NSLayoutConstraint crash
- From: Jack Brindle <email@hidden>
- Date: Fri, 16 Mar 2018 10:12:47 -0700
I would be willing to bet that you are throwing a lot of constraint exceptions
without realizing it.
When the view is laid out the constraints are evaluated, including the
priorities. When a set
of constraints has a conflict with another (very easy to do) the priorities
come into play. If
you set the constraints in IB and do not set the priorities, then they will
usually be the same.
The view system will then have to take a guess at what you really wanted to do,
breaking
one of the constraints (usually the very one you find most important). This
will be
displayed in the log as a constraint exception. Many times it will take several
layout
passes before it finds a setup it can work with. Reading the constraint display
information
is an art, but one that can be learned and is then very valuable.
It turns out that the key to really understanding and working with constraints
is in setting
priorities properly. This takes a while to figure out, but it appears to be
something you
are about to go through. As you lay out the views and set the constraints, try
to make
sure that there is only a single set of constraints on each view, and then if
there are
more than one, the priorities are set so that the views will display where you
want
them to be.
There is another thing that most developers don’t realize - the view system
will create
constraints behind your back if you let it. It will use the autoresizing mask
to generate
constraints, which means there is an extra set of constraints that can
interfere with
what you really want. In most of my view controllers in either the viewDidLoad
or
awakeFromNib you will find the line:
view.translatesAutoresizingMaskIntoConstraints = NO;
This allows me to only have the constraints I set, eliminating a lot of issues.
There are a few NSView methods that will dump the constraints in place for the
view:
constraints
will return the constraints for a view. This is all the constraints (in an
NSArray) and
will show you everything, including some not so useful things.
After a layout has occurred, use:
constraintsAffectingLayoutForOrientation:
The NSArray of constraints will include the ones you really want to look at for
the view. This is the one you really want to use to see what is actually going
on.
Constraints is a very big topic with many things to learn and control. They are
very powerful, and thus very maddening when they don’t work they way you
understand. Take the time to learn how to use them, and you will be much
happier!
- Jack
> On Mar 16, 2018, at 9:40 AM, David Catmull <email@hidden> wrote:
>
> I might try it, but it's difficult to do accurately because the views are
> assembled programmatically - it's dynamically generated based on the data
> read in.
>
> On Fri, Mar 16, 2018 at 10:37 AM, Richard Charles <email@hidden>
> wrote:
>
>>
>>> On Mar 16, 2018, at 9:03 AM, David Catmull <email@hidden>
>> wrote:
>>>
>>> After I set up a somewhat complex view hierarchy, I'm getting a crash
>> with
>>> this exception:
>>>
>>> 2018-03-16 08:59:21.814873-0600 App[31201:13046721] *** Assertion
>> failure
>>> in -[NSLayoutConstraint setPriority:],
>>> /BuildRoot/Library/Caches/com.apple.xbs/Sources/Foundation/
>> Foundation-1451/Foundation/Layout.subproj/NSLayoutConstraint.m:222
>>>
>>> It happens on the main event loop where none of my code is involved; I
>>> never set constraint priorities myself. What could be causing this error?
>>
>> Have you tried manually exercising the constraints? In Interface Builder
>> drag the views into random positions and sizes then click "Update Frames”
>> to force the constraint system to apply the constraints. This may uncover
>> any errors you have in the constraints containted in the view hierarchy.
>>
>> --Richard Charles
>>
>>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden