bright / Cero

HTML like views for iOS
Apache License 2.0
3 stars 1 forks source link

Support for auto layout #1

Closed miensol closed 9 years ago

miensol commented 9 years ago

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):

<UIView>
    <UIButton id="myButton" layout_width="match_parent" layout_height="match_parent" layout_center="horizontal | vertical | true">
    </UIButton>
</UIView>
NOtherDev commented 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:

This gives something like this in most cases:

 <Constraint on="NSLayoutAttributeLeft" with="#anotherControl" />

And this looks OK for me.

More ideas:

miensol commented 9 years ago

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)"> ?

miensol commented 9 years ago

Most of commonly used costructs is supported now.