6

I need to change the UITabBar height to 95. I can do that in the older version of iOS Swift. This is my code that work in the older version.

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    tabBar.frame.size.height = 95
    tabBar.frame.origin.y = view.frame.height - 95

    menuButton.frame.origin.y = self.view.bounds.height - tabBar.frame.size.height - 10
    shadowBtn.frame.origin.y = self.view.bounds.height - tabBar.frame.size.height - 15
}
Pratik Sodha
  • 3,679
  • 2
  • 19
  • 38
Fabio Cirruto
  • 71
  • 1
  • 7

2 Answers2

21

Try it in viewDidLayoutSubviews

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    tabBar.frame.size.height = 95
    tabBar.frame.origin.y = view.frame.height - 95
}
Kuldeep
  • 4,466
  • 8
  • 32
  • 59
1

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

Nikunj Acharya
  • 767
  • 8
  • 19