Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@"self" implicit view? #5

Closed
peternlewis opened this issue Apr 15, 2014 · 4 comments
Closed

@"self" implicit view? #5

peternlewis opened this issue Apr 15, 2014 · 4 comments

Comments

@peternlewis
Copy link

It seems that when called as a category on UIView, it would be nice to have @"self" as an implicit view (like @"super"), so that instead of this:

[_containerView addCompactConstraint:@"container.width = 320" metrics:nil views:@{@"container" : i_containerView}];

you could have:

[_containerView addCompactConstraint:@"self.width = 320" metrics:nil views:nil];

I would implement this by adding an (optional) selfView parameter to the NSLayoutConstraint category methods and using that from the UIView category methods, but it could also be done by having the UIView category "mutate" the views dictionary. The first method seems better, but it depends on how much you dislike adding either the parameter or more methods.

@marcoarment
Copy link
Owner

I'm actually pretty new to autolayout (yeah, I know), but from what I understand, constraints are supposed to be applied to the superview. Is it reasonable practice to apply a constraint to self like that, or should that be applied to _containerView.superview?

Some autolayout-helper libraries allow you to apply constraints directly to a view, e.g.:

[c addCompactConstraint:@"self.width = 320"];

would do this behind the scenes:

[c.superview addCompactConstraint:@"c.width = 320" metrics:nil views:@{@"c" : c}];

...but CompactConstraint has (so far) intentionally not done that. If we add support for self, it would make sense to also require self.superview to be set and actually add the constraint to the superview instead of self, right?

(Not sure that's a good idea yet, but worth considering.)

@peternlewis
Copy link
Author

I'm afraid this was my first use of autolayout as well, so I can't answer with confidence, other than to say the code I quoted works, it correctly sets the width constraint on the view. That said, it works fine with I use _containerView.superview as well:

[_containerView.superview addCompactConstraint:@"container.width = 320" metrics:nil views:@{@"container" : _containerView}];

The documentation for addConstraint says the constraints may only reference the view or its subviews.

I'm not sure, but I would have thought that both these

[c addCompactConstraint:@"self.width = 320"];
[c.superview addCompactConstraint:@"c.width = 320" metrics:nil views:@{@"c" : c}];

would do exactly the same thing, but no, they don't. They both add the same constraint, but they do indeed add it to the different views.

Presumably the layout system collects the constraints as it descends so that they are functionally equivalent. The difference would be if the view was removed from the superview, then the constraints of the superview referring to it are removed as well (I just checked this).

So it seems to me its the users responsibility to ensure that the receiver is the correct view to add the constraint to. It also seems to me there are really two case for the receiver: it is the one and only view that is being configured (eg for width as above), or it is the superview of all the views in the views array. In either case "self" is useful, referring to the one view and avoiding the dictionary, or referring to the superview which is likely useful for setting the constraints.

But all that said, I'm a total novice at this, so I definitely could be completely wrong.

@marcoarment
Copy link
Owner

Let's give it a try.

@peternlewis
Copy link
Author

Looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants