Disable Autolayout Constraint Error Messages in Debug Console Output in Xcode

Disable autolayout constraint error messages in debug console output in Xcode

Did some decompiling and there's actually a way:

for Swift 5

UserDefaults.standard.set(false, forKey: "_UIConstraintBasedLayoutLogUnsatisfiable")

Objective-C

[[NSUserDefaults standardUserDefaults] setValue:@(NO) forKey:@"_UIConstraintBasedLayoutLogUnsatisfiable"];

Now you'll only get notified that "_UIConstraintBasedLayoutLogUnsatisfiable is OFF".

Obligatory reminder for anyone reading this: this should be last resort, for tips on debugging and fixing constraint issues see WWDC's "Mysteries of Auto Layout" sessions: part 1 and part 2.

How can I make AutoLayout crash my app when it logs unsatisfiable constraints in debug console?

Add a symbolic breakpoint on UIViewAlertForUnsatisfiableConstraints.

Now if you get a constraint conflict, at least the app will stop and you’ll notice.

Can you label views in AutoLayout console output without using Storyboards?

I would love to get a clear picture of which is actually causing the problem.

I have two suggestions.

  1. In Xcode, during the same run of the app (preferably right after you get are given some sort of console dump about the constraints), choose Debug -> View Debugging -> Capture View Hierarchy.

    The app pauses and you are shown your view hierarchy. Select a view and you are shown its memory address in the Object Inspector. When you find the one whose address is 0x8675309, that's the one. Plus you can see the constraints that position this view in the Size Inspector.

  2. Implement the constraint logger utilities that I provide in my book. The second one reports the view hierarchy along with the identifiers and the constraints.

Here are the utilities; between them, I've saved hours of puzzlement:

extension NSLayoutConstraint {
class func reportAmbiguity (var v:UIView?) {
if v == nil {
v = UIApplication.sharedApplication().keyWindow
}
for vv in v!.subviews as [UIView] {
println("\(vv) \(vv.hasAmbiguousLayout())")
if vv.subviews.count > 0 {
self.reportAmbiguity(vv)
}
}
}
class func listConstraints (var v:UIView?) {
if v == nil {
v = UIApplication.sharedApplication().keyWindow
}
for vv in v!.subviews as [UIView] {
let arr1 = vv.constraintsAffectingLayoutForAxis(.Horizontal)
let arr2 = vv.constraintsAffectingLayoutForAxis(.Vertical)
NSLog("\n\n%@\nH: %@\nV:%@", vv, arr1, arr2);
if vv.subviews.count > 0 {
self.listConstraints(vv)
}
}
}
}

Constraints warning in console

Unsatisfiable Layouts

Unsatisfiable layouts occur when the system cannot find a valid solution for the current set of constraints. Two or more required constraints conflict, because they cannot all be true at the same time.

Identifying Unsatisfiable Constraints

Often, Interface Builder can detect conflicts at design time. ON these occasions, Interface builder displays the error in a number of ways:

  • All the conflicting constraints are drawn on the canvas in red.

  • Xcode lists the conflicting constraints as warnings in the issue
    navigator.

  • Interface Builder displays a red disclosure arrow in the upper right
    corner of the document outline.

Unsatisfiable Constraints

Click the disclosure arrow to display a list of all the Auto Layout issues in the current layout.

Interface Builder can often recommend fixes for these issues. For more information, see Resolving Layout Issues for a View Controller, Window, or Root View in Auto Layout Help.

Below is the link where Apple has specified all of this. Please do read the documentation. It has most of the answers a beginner might want.

Source: Apple Documentation



Related Topics



Leave a reply



Submit