NSLayoutConstraint allows creating non-width/height constraints without a second item

Originator:argentumko
Number:rdar://19804930 Date Originated:12-Feb-2015
Status:Closed Resolved:Yes
Product:iOS SDK Product Version:iOS 7.0+
Classification:Other Bug Reproducible:Always
 
Summary:
There's an undocumented behavior of NSLayoutConstraint which allows users of this class to manually create constraints in code without specifying the second item of the constraint ("unary constraints"), while first attribute is not width or height. Adding such constraint to a view effectively constrains the first attribute of the first item to a virtual (0,0) point of the window of that view, offset by the specified constant.
Interestingly, this only works if the second attribute is specified, but the attribute itself doesn't matter. It will even become "not an attribute" once constraint is created, but the factory method throws an exception if "not an attribute" is specified for the second attribute.

Steps to Reproduce:
1. Create a view in Interface Builder on in code. Make sure it has intrinsic content size or its width and height are constrained.
2. In code, create and add NSLayoutConstraint object with that view as a first item, nil for second item, and any valid attribute as the second attribute. For example:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.topLeftView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeTop multiplier:1.0 constant:100.0f]];

Expected Results:
I would expect an exception to be raised by NSLayoutConstraint factory method if its user attempts to create "spacing" or "alignment" constraints without a second item. If this scenario is, in fact, valid (and provides a way to do "absolute positioning"), then I would expect it to be properly documented.

Actual Results:
View appears constrained by the specified first attribute to a virtual (0,0) point of its window, offset by the specified constant.

Version:
Reproducible at least on iOS 7 and iOS 8.

Configuration:
Reproducible on all devices and simulators.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!