swiftuikitsnapkit

How to get off the floor by half the height of View using a snapkit


enter image description here

'UIView' is displayed using the snapkit (assuming 200 heights)

Use "equalToSuperview" to secure it to the floor

I want to lower the Y axis of the UI view to half the height.

self.tempView.snp.makeConstraints {
    $0.leading.equalToSuperview().offset(-50)
    $0.trailing.equalToSuperview().offset(50)
    $0.height.equalTo(self.tempView.snp.width)
    $0.bottom.equalToSuperview().offset(self.tempView.snp.height / 2)
}

Solution

  • You want the bottom of tempView to be superview.bottom + tempView.height / 2. Notice that this is the same as saying that the centre Y of tempView should be equal to the bottom of its superview.

    make.centerY.equalTo(yourSuperview.snp.bottom)
    

    If the offset is another ratio of the height, like 1/3 of the height, then I would use another view, placed at the bottom of the superview, to help:

    // anotherView should be added as a sibling of tempView
    anotherView.snp.makeConstraints { make in
        make.top.equalTo(yourSuperview.snp.bottom)
        make.horizontalEdges.equalToSuperview()
        make.height.equalTo(tempView.snp.height).dividedBy(3)
    }
    
    tempView.snp.makeConstraints { make in
        // ...
        make.bottom.equalTo(anotherView.snp.bottom)
    }