Reputation: 11529
EDIT: I updated the question so it is more clear.
Is there a way to check the constraints and get an NSArray
of conflicting ones?
I'm working on an Autolayout helper class and I would like to print a pretty log when there are conflicts or errors.
By default, when there are layout conflicts you get a log that shows the conflicting constraints like this:
"<NSLayoutConstraint:0x7fe110638610 UILabel:0x7fe11062bb80'green'.leading == UIView:0x7fe11070de50.leadingMargin>",
"<NSLayoutConstraint:0x7fe1106386b0 H:[UILabel:0x7fe11062bb80'green'(20)]>",
"<NSLayoutConstraint:0x7fe110638660 UIView:0x7fe11070de50.trailingMargin == UILabel:0x7fe11062bb80'green'.trailing>",
"<NSLayoutConstraint:0x7fe11063fbe0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fe11070de50(375)]>"
I would like to get those constraints to display them in a prettier way (by view key for example). Any idea on how to do it?
Thanks!
Upvotes: 0
Views: 417
Reputation: 535306
My book provides a couple of utility methods (implemented in a category on NSLayoutConstraint) that I like to use to log constraints when I'm debugging or exploring them:
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)
}
}
}
}
Upvotes: 1