View that sticks to bottom of screen

Today we are going to see how to stick view to bottom of screen.We use programmatic approach using auto layout.

Stickyview at bottom of view

First we create label with some text, say “Offline mode”.Then we are going to put it at bottom of screen.

var lblOffline = UILabel()

Then in viewDidLoad() method, we add view to bottom of screen.

fileprivate func setupName(){
let height = CGFloat(50)
//set up label properties
lblOffline.text = "Offline mode"
lblOffline.backgroundColor = .red
lblOffline.textAlignment = .center
//Step 1
lblOffline.translatesAutoresizingMaskIntoConstraints = false
//Step 2
view.addSubview(lblOffline)
//Step 3
NSLayoutConstraint.activate([
lblOffline.leadingAnchor.constraint(equalTo:view.safeAreaLayoutGuide.leadingAnchor),
lblOffline.trailingAnchor.constraint(equalTo:view.safeAreaLayoutGuide.trailingAnchor),
lblOffline.topAnchor.constraint(equalTo:view.safeAreaLayoutGuide.bottomAnchor,constant: -height),
lblOffline.bottomAnchor.constraint(equalTo:view.safeAreaLayoutGuide.bottomAnchor),
])
}

That’s how it’s done.

Thanks for reading. Please feel to ask if you have any queries.

If you like this, please clap and support me.