I am using following approach
extension UIWindow {
static var key: UIWindow? {
if #available(iOS 13, *) {
return UIApplication.shared.windows.first { $0.isKeyWindow }
} else {
return UIApplication.shared.keyWindow
}
}
}
extension UITabBar {
override open func sizeThatFits(_ size: CGSize) -> CGSize {
super.sizeThatFits(size)
guard let window = UIWindow.key else {
return super.sizeThatFits(size)
}
var sizeThatFits = super.sizeThatFits(size)
sizeThatFits.height = window.safeAreaInsets.bottom + <# Height #>
return sizeThatFits
}
}
Or if you don't like to create extension, create UITabBar Subclass and then override this method.
When you are changing height of Tab bar with viewWillLayoutSubviews
and viewDidLayoutSubviews
, you are forgetting the Safe Area. You will have to set addition Safe Area Inset via self.additionalSafeAreaInsets
and by doing so this inside viewDidLayoutSubviews
and viewWillLayoutSubview
, Tab bar will shift upwards (because you have added additional inset). If you don't care about Auto Layout and Safe Area, then you are fine to go with viewWillLayoutSubviews
and viewDidLayoutSubviews