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.
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.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.
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
Add a Button on Right View of Uitextfield in Such Way That, Text Should Not Overlap the Button
After Upgrading to Xcode 11.2 from Xcode 11.1, App Crashes Due to _Uitextlayoutview
What Tool(S) How to Use to Produce iPhone App Screencasts
Warning: Attempt to Present * on * Whose View Is Not in the Window Hierarchy - Swift
Convert Mkcoordinateregion to Mkmaprect
Share Extension to Open Containing App
Facebook Login - iOS 9 - Without Safari
Auto Adjust Custom Uitableviewcell and Label in It to the Text
Swiftui Holding Reference to Deleted Core Data Object Causing Crash
Add Swipe to Delete Uitableviewcell
Uiscrollview with Centered Uiimageview, Like Photos App
Uicollectionview Adding Uicollectioncell
The Maximum Number of Apps for Free Development Profiles Has Been Reached. Xcode 11.5