KeepLayout icon indicating copy to clipboard operation
KeepLayout copied to clipboard

API redesign

Open Tricertops opened this issue 13 years ago • 12 comments

Keep Layout was created to simplify creating constraints in code. It is a wrapper around to this class method +constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant: of NSLayoutConstraint (and -priority property). This default constructor provides huge number of possible attribute combinations with different multipliers, constants, views, relations and priorities.

The fact is, that not all combinations are useful or even valid. You cannot relate width to the lelf edge or it makes no sense to use constant and muliplier at the same time when constraining center alignment.

I wanted to reduce this number of combinations by eliminating such invalid or not used possibilities. This was my first attempt – to create abstraction above related attributes to in KeepAttribute class and create value with priority in KeepRule class. And it works… (see Issues for possible bugs :innocent:).


The next step will be to extract most used cases of keeping attributes to dedicated methods. Let's say we will have method [view keepTopFrom:10 to:20] or [view keepCentered]. (I started this topic in Issue #2.)

I would like _you_ (yes, you, who is read this and probably using this library) to write:

What are your most used cases?

Feel free to suggest your ideas of simplified API.

Tricertops avatar Mar 10 '13 21:03 Tricertops

Maybe a method to keep the inset top left right bottom Min to a value ? Because I feel like I'm writing that part al ot like 4 times for each UIView/UILabel etc...

ClarisseL avatar Mar 10 '13 21:03 ClarisseL

Yes, setting all 4 insets is reasonable, but do you set fixed or minimum or maximum value? What priorities do you use?

I would like to use UIEdgeInsets for this, but how to handle the case when you want to set only 3 of 4 insets?

Tricertops avatar Mar 10 '13 21:03 Tricertops

I'm really not an advanced developer I'm learning so I don't think I'll be of any help but for the Inset maybe make one method that set the Min for the 4 inset ?

For example all my insets have a Min of 10 and sometimes Equal 10

ClarisseL avatar Mar 10 '13 21:03 ClarisseL

Just an idea of new methods:

[view.keepTopInset keep:15 min:10 max:20];
[view.keepAspectRatio mustKeep:4/3.];
[view.keepLeftInset remove];

Each view has the attributes accessible using direct method. The attribute object is created lazily and is associated with the view. This means you can access the same attribute object later, change its values or remove it completely.

There could be a proxy attribute for all 4 insets:

[view.keepAllInsets keep:15 min:10 max:10];

This would forward the message to all 4 inset atributes.

Tricertops avatar Mar 14 '13 12:03 Tricertops

Oh yeah that sounds really nice !

ClarisseL avatar Mar 14 '13 13:03 ClarisseL

Oh yeah that sounds really nice ![2], I also think when you use

[view.keepTopInset keep:15 min:10 max:20]; //(for example)

and it's already setted it could just edit it.

lucoceano avatar Mar 15 '13 00:03 lucoceano

Would be quite nice a method like this

[view keepTop:10 bottom: 15 left:13 right: 30];

lucoceano avatar Mar 15 '13 00:03 lucoceano

Yes, the method -keepTopInset will create or return existing attribute object, so after calling the method -keep:min:max: it would modify the existing values (and modify the constraint as well):

NSLayoutConstraint class, constant property discussion: Unlike the other properties, the constant may be modified after constraint creation. Setting the constant on an existing constraint performs much better than removing the constraint and adding a new one that's just like the old but for having a new constant.

But instead of method like this:

[view keepTop:10 bottom: 15 left:13 right: 30];

I would prefer something like this:

[view.keepAllInsets mustKeep:UIEdgeInsetsMake(10, 13, 15, 30)];

Because of consistency of the interfaces (all atributes are set the same way) and we use existing UIEdgeInsets structure, so it may be easier to work with one struct, than 4 related floats.

Also, I will have to be com complex than just set the value. These constraints are based on priorities, so each value you specify should have a priority. The methods I posted above gives your values some implicit priorities:

  • -keep:min:max: - keep value will have shall priority, min and max will have must (meaning just like in existing implementation)
  • -mustKeep: - the value will have must priority and it will be min and max at the same time
  • -shallKeep:, -mayKeep: - similar to -mustKeep:

By the way, do you like the naming of priorities: must, shall, may, fit? Do you have any better ideas? I don't like the last fit, but I can't find any weaker modal verb than may in English :laughing:

Tricertops avatar Mar 15 '13 07:03 Tricertops

hi, actually I really like your method they're very intuitive, your library is very good and with these changes it will even better.

lucoceano avatar Apr 02 '13 19:04 lucoceano

I haven't really looked into the nitty gritty of NSLayoutConstraint, and as such I'm probably breaking a paradigm somewhere or missing something obvious, but I find that I often have to constrain the height and width of every element to whatever its default height and width are (usually the size of it's frame).

Unless I'm using it wrong (very high possibility), it'd be nice to see

[self.button keep:[KeepHeight rules:@[ [KeepEqual must:self.button.frame.size.height] ]]];
[self.button keep:[KeepWidth rules:@[ [KeepEqual must:self.button.frame.size.width] ]]];

become something like

[self.button keepSize:CGSizeMake(100, 100)];

kylehughes avatar Apr 12 '13 21:04 kylehughes

You guys (and girls too) who are interested, check out the release/1.0.0 branch. I've started re-implementing the whole thing in much better, easier and more powerful way. Sneak peek:

view.keepAllInsets.equal = KeepRequired(10);

Tricertops avatar Jun 19 '13 18:06 Tricertops

It's done. Version 1.0 is out, go and check it. :clap:

Tricertops avatar Jun 25 '13 20:06 Tricertops