For the More Curious: NSAutoresizingMaskLayoutConstraint

As we mentioned earlier, before Auto Layout iOS applications used another system for managing layout: autoresizing masks. Each view had an autoresizing mask that constrained its relationship with its superview, but this mask could not affect relationships between sibling views.

By default, views create and add constraints based on their autoresizing masks. However, these translated constraints often conflict with the explicit constraints in your layout, which results in an unsatisfiable constraints problem.

To see this happen, comment out the line in loadView() that turns off the translation of autoresizing masks.

segmentedControl.translatesAutoresizingMaskIntoConstraints = false
// segmentedControl.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(segmentedControl)

Now the segmented control has a resizing mask that will be translated into a constraint. Build and run the application and navigate to the map interface. You will not like what you see. The console will report the problem and its solution.

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:0x7fb6b8e0ad00
      h=--& v=--& H:[UISegmentedControl:0x7fb6b9897390(212)]>",
    "<NSLayoutConstraint:0x7fb6b9975350 UISegmentedControl:0x7fb6b9897390.leading
      == UILayoutGuide:0x7fb6b9972640'UIViewLayoutMarginsGuide'.leading>",
    "<NSLayoutConstraint:0x7fb6b9975460 UISegmentedControl:0x7fb6b9897390.trailing
      == UILayoutGuide:0x7fb6b9972640'UIViewLayoutMarginsGuide'.trailing>",
    "<NSLayoutConstraint:0x7fb6b8e0b370 'UIView-Encapsulated-Layout-Width'
      H:[MKMapView:0x7fb6b8d237c0(0)]>",
    "<NSLayoutConstraint:0x7fb6b9972020 'UIView-leftMargin-guide-constraint'
      H:|-(0)-[UILayoutGuide:0x7fb6b9972640'UIViewLayoutMarginsGuide'](LTR)
      (Names: '|':MKMapView:0x7fb6b8d237c0 )>",
    "<NSLayoutConstraint:0x7fb6b9974f50 'UIView-rightMargin-guide-constraint'
      H:[UILayoutGuide:0x7fb6b9972640'UIViewLayoutMarginsGuide']-(0)-|(LTR)
      (Names: '|':MKMapView:0x7fb6b8d237c0 )>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fb6b9975460 UISegmentedControl:0x7fb6b9897390.trailing
  == UILayoutGuide:0x7fb6b9972640'UIViewLayoutMarginsGuide'.trailing>

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.

Let’s go over this output. Auto Layout is reporting that it is Unable to simultaneously satisfy constraints. This happens when a view hierarchy has constraints that conflict.

Then, the console spits out some handy tips and a list of all constraints that are involved, with their descriptions. Let’s look at the format of one of these constraints more closely.

<NSLayoutConstraint:0x7fb6b9975350 UISegmentedControl:0x7fb6b9897390.leading
  == UILayoutGuide:0x7fb6b9972640'UIViewLayoutMarginsGuide'.leading>

This description indicates that the constraint located at memory address 0x7fb6b9975350 is setting the leading edge of the UISegmentedControl (at 0x7fb6b9897390) equal to the leading edge of the margin of the UILayoutGuide (at 0x7fb6b9972640).

Five of the affected constraints are instances of NSLayoutConstraint. One, however, is an instance of NSAutoresizingMaskLayoutConstraint. This constraint is the product of the translation of the image view’s autoresizing mask.

Finally, Auto Layout tells you how it is going to solve the problem by listing the conflicting constraint that it will ignore. Unfortunately, it chooses poorly and ignores one of your explicit instances of NSLayoutConstraint instead of the NSAutoresizingMaskLayoutConstraint. This is why your interface looks the way it does.

The note before the constraints are listed is very helpful: The NSAutoresizingMaskLayoutConstraint needs to be removed. Better yet, you can prevent this constraint from being added in the first place by explicitly disabling translation in loadView():

// segmentedControl.translatesAutoresizingMaskIntoConstraints = false
segmentedControl.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(segmentedControl)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.116.20