Giter VIP home page Giter VIP logo

Comments (4)

marcoarment avatar marcoarment commented on June 24, 2024

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

from compactconstraint.

peternlewis avatar peternlewis commented on June 24, 2024

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.

from compactconstraint.

marcoarment avatar marcoarment commented on June 24, 2024

Let's give it a try.

from compactconstraint.

peternlewis avatar peternlewis commented on June 24, 2024

Looks good to me.

from compactconstraint.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.