Closed miensol closed 9 years ago
General format for the constraint is:
/* accessors
firstItem.firstAttribute {==,<=,>=} secondItem.secondAttribute * multiplier + constant
*/
@property (readonly, assign) id firstItem;
@property (readonly) NSLayoutAttribute firstAttribute;
@property (readonly) NSLayoutRelation relation;
@property (readonly, assign) id secondItem;
@property (readonly) NSLayoutAttribute secondAttribute;
@property (readonly) CGFloat multiplier;
@property CGFloat constant;
@property UILayoutPriority priority;
Ideally we should support all of it, so the structure might look like:
<UIView>
<UIButton id="myButton">
<Constraints>
<Constraint on="NSLayoutAttributeLeft" relation="NSLayoutRelationEqual"
with="#anotherControl.NSLayoutAttributeLeft" multiplier="0.8"
constant="100" priority="9" />
</Constraints>
</UIButton>
</UIView>
But this is awfully verbose. Some defaults may be introduced:
NSLayoutRelationEqual
if not specifiedwith
, another control attribute may be assumed to be the same as on
value if omittedThis gives something like this in most cases:
<Constraint on="NSLayoutAttributeLeft" with="#anotherControl" />
And this looks OK for me.
More ideas:
NSLayoutAttribute
part - it can be just <Constraint on="left" with="#anotherControl" />
.<UIButton constraints="fillParentEdges">
- this would mimic helper methods known from AutoLayout etc. and allow skipping the whole <Constraints>
element.Here is what so far is working:
<UIView>
<Constraint on="width,height" with=":superview"/>
<UILabel text="First label which become longer" id="firstLabel">
<Constraint on="topMargin" with=":superview" constant="40" id="topMarginConstraint" />
<Constraint on="width" with=":superview" constant="-20"/>
<Constraint on="centerx" with=":superview"/>
</UILabel>
<UIButton>
<Constraint on="left" with="#firstLabel"/>
<Constraint on="width" with=":previous" multiplier="0.5" constant="-10"/>
<Constraint on="top" with="#firstLabel.bottom" constant="30"/>
</UIButton>
<UIButton>
<Constraint on="width,top" with=":previous" id="buttonWidthAndTopConstraint" />
<Constraint on="left" with=":previous.right" constant="20"/>
</UIButton>
</UIView>
I'm still not sure about the shortcut form <UIButton constraints="fillParentEdges">
. It's obviously more readable than <Constraint on="width,height,top,left" with=":superview"/>
.
How would we define a padding? Maybe this would work
<UIButton constraints="fillParentEdges(10,20,10,20)">
?
Most of commonly used costructs is supported now.
Obviously we would like an easy way to layout our views. The question is how we will declare layout configuration? Let's discuss them here.
For simplicity let's assume we declare them inline and we can reference other views with
#id
An example (which I don't like):