If you use AutoLayout
and want to keep your custom tab bar at the bottom of the screen while taking into account the safe area on iPhone X for example, you can simply use view.safeAreaLayoutGuide.bottomAnchor
to pin your custom view to the bottom of the screen.
For example:
let myCustomTabBar = UIView()
// ...
view.addSubview(myCustomTabBar)
myCustomTabBar.translatesAutoresizingMaskIntoConstraints = false
myCustomTabBar.heightAnchor.constraint(equalToConstant: 80).isActive = true
myCustomTabBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
myCustomTabBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
myCustomTabBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
If you want to know the safe area insets for your window (without taking into account your tab bar or navigation bar) you can access the UIWindow
's safeAreaInsets
property, or check out its safeAreaLayoutGuide.layoutFrame
.
You can also override the viewSafeAreaInsetsDidChange
of your UIViewController
and make the needed adjustments depending on what behaviour you're looking for.