Search code examples
iosswiftsnapkit

Snapkit, stick UILabel to bottom


I have a scrollview with a couple of elements inside. I must set the bottom label to stick to the bottom of the view no matter what but should not overlap with the view on top of it which happens when the screen is small.

How do I achieve this in Snapkit?


Solution

  • Swift 4:

    someScrollView.translatesAutoresizingMaskIntoConstraints = false
    
    let stickyLabel = UILabel()
    stickyLabel.backgroundColor = UIColor.red
    stickyLabel.translatesAutoresizingMaskIntoConstraints = false
    stickyLabel.text = "some sticky bottom label"
    self.view.addSubview(stickyLabel)
    
    someScrollView.snp.makeConstraints { (make) -> Void in
        make.top.leading.trailing.equalToSuperview()
    }
    
    stickyLabel.snp.makeConstraints { (make) -> Void in
        make.top.equalTo(someScrollView.snp.bottom).offset(4)
        make.leading.equalToSuperview().offset(4)
        make.bottom.trailing.equalToSuperview().offset(-4)
    }
    

    would give something like:

    sticky bottom view