9

I'm using Snapkit to simplify my autolayout code, however one scenario seems to popup very regularly, which i'm wondering if there's a way which involves less code.

So let's say that I need to pin the edges of a UIView to it's superview margins, we might do something like this:

subView.snp.makeConstraints { make in
    make.top.equalTo(parentView.snp.topMargin)
    make.bottom.equalTo(parentView.snp.bottomMargin)
    make.left.equalTo(parentView.snp.leftMargin)
    make.right.equalTo(parentView.snp.rightMargin)
}

This essentially results in the subview filling the parent view, except for a small amount of padding as defined by the parent views layout margins.I'm sure some variation of this is pretty common.

This seems overly verbose for this library. It has some really nice helper methods such as these

make.edges.equalToSuperview()
make.top.left.right.equalToSuperview()

What I haven't managed to find in their documentation however is how to do the two above helper methods, in relation to the margins.

What i'm looking for (if it exists) is something akin to:

make.edges.equalToSuperview().withMargins()
make.top.left.right.equalToSuperview().withMargins()
make.top.left.right.equalTo(someview).withMargins()

So, is there a way of doing this other than the very verbose way? Am I missing something in the documentation or maybe this could be added by extension?

TRG
  • 845
  • 1
  • 9
  • 22

2 Answers2

16

did you try something like this?

subView.snp.makeConstraints { make in
    make.edges.equalTo(view.snp.margins)
}

Edit after comment:

When you only want to constrain certain edges to the superview margin, you can do something like this.

subView.snp.makeConstraints { make in
    make.top.leading.equalTo(view).inset(view.layoutMargins)
}

or

subView.snp.makeConstraints { make in
    make.top.leading.equalTo(view.layoutMarginsGuide)

or

subView.snp.makeConstraints { make in
    make.top.leading.equalTo(view.safeAreaLayoutGuide)
JustinM
  • 2,202
  • 2
  • 14
  • 24
  • Well that solves half the problem and I can start cleaning up my code. But that only works if your doing all edges. `make.top.left.equalTo(view.snp.margins)` won't work as they are mismatched. Maybe I need to write an extension to support that function, just seems like a really common thing to do so I expected a more elegant solution already in the library. – TRG Sep 11 '17 at 14:32
  • @TRG check edit for what you can do in that situation. – JustinM Sep 12 '17 at 00:52
  • 1
    Important thing to remember when using `...inset(view.layoutMargins` is that this creates a constant based on the value of layoutMargins. If the layout margins change after constraint creation (likely in a tableview) they won't be updated. – arsenius Jul 27 '18 at 01:18
2

One nice way to do this is to use UIView.layoutMarginsGuide:

childView.snp.makeConstraints { make in
    make.top.leading.bottom.equalTo(parentView.layoutMarginsGuide)
    make.trailing.equalTo(otherView.snp.leading).offset(-8.0)
}
arsenius
  • 12,090
  • 7
  • 58
  • 76