Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SheldonWangRJT/2ac5fe14257adc8680d2284ebaea7c1f to your computer and use it in GitHub Desktop.
Save SheldonWangRJT/2ac5fe14257adc8680d2284ebaea7c1f to your computer and use it in GitHub Desktop.
iOS/MacOS Tricks to Debug/Detecting Ambiguous Auto Layouts Issues

iOS/MacOS Tricks to Debug/Detecting Ambiguous Auto Layouts Issues

#iOSBySheldon #AutoLayout

This notes is written by Sheldon. You can find me with #iOSBySheldon in Github, Youtube, Facebook, etc.

This time I will bring you some tricks directly from Apple’s document regarding the ways to debug / detecting ambiguous warning (⚠️) for Auto Layouts in iOS and MacOS. To kick things off, I do want mention that Ambiguous Warning for Auto Layout means that the configuration you add into your storyboard or xib file has no conflicts (otherwise it will show ❗️in storyboard). However, it has multiple ways to fulfill your auto layout, so that the system will randomly choose a fine solution to render your view, which maybe correct, maybe wrong, in other words, very inconsistent.

“There are a few methods you can call to help identify ambiguous layouts. All of these methods should be used only for debugging. Set a breakpoint somewhere where you can access the view hierarchy, and then call one of the following methods from the console:

  • hasAmbiguousLayout. Available for both iOS and OS X. Call this method on a misplaced view. It returns YES if the view’s frame is ambiguous. Otherwise, it returns NO.
  • exerciseAmbiguityInLayout. Available for both iOS and OS X. Call this method on a view with ambiguous layout. This will toggle the system between the possible valid solutions.
  • constraintsAffectingLayoutForAxis:. Available for iOS. Call this method on a view. It returns an array of all the constraints affecting that view along the specified axis.
  • constraintsAffectingLayoutForOrientation:. Available for OS X. Call this method on a view. It returns an array of all the constraints affecting that view along the specified orientation.
  • _autolayoutTrace. Available as a private method in iOS. Call this method on a view. It returns a string with diagnostic information about the entire view hierarchy containing that view. Ambiguous views are labeled, and so are views that have translatesAutoresizingMaskIntoConstraints set to YES.”

Note: You may need to use Objective-C syntax when running these commands in the console. For example, after the breakpoint halts execution, type call [self.myView exerciseAmbiguityInLayout] into the console window to call the exerciseAmbiguityInLayout method on the myView object. Similarly, type po [self.myView autolayoutTrace] to print out diagnostic information about the view hierarchy containing myView.

Reference Links: [1]https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/AmbiguousLayouts.html#//apple_ref/doc/uid/TP40010853-CH18-SW1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment