iOS Dev Nugget 286 Use Auto Layout for Bottom Bar


Need to run a code review on your codebase? Hire me

A common pattern for UI layout is to have a bar at the bottom of the screen like UITabBar. It has a fixed height in most cases, but with the iPhone X, you want it to stretch to the bottom of the screen, outside of the safe area while keeping it's contents (in UITabBar's case, the tab buttons) glued to the top of the bar.

Auto Layout anchors provide a very simple way to wire this up. In your view controller:

let buttonsHeight = 44
footerBar.topAnchor.constraint(equalTo: view.layoutGuide.bottomAnchor, constant: -buttonsHeight),
footerBar.bottomAnchor.constraint(equalTo: view.bottomAnchor),

The trick is to anchor the top and bottom of footerBar.

Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No



Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter


View archives of past issues