How to change tab bar height programmatically

2019-08-29 01:45发布

问题:

I want to change the height of an tab bar. I changed it this way, but no change occurred.

Here's my UITabBarController:

import UIKit
import SideMenu

class TabBarController: UITabBarController {

override func viewDidLoad() {
    super.viewDidLoad()
    setupSideMenu()
    self.navigationController?.navigationBar.isHidden = true

    self.tabBar.frame = CGRect(
        origin: CGPoint(x: 0, y: 20),
        size: CGSize(width: 400, height: 200)
    )
}

回答1:

I am using an extension for changing height of tab-bar

 class CustomHeightTabBar : UITabBar {
        @IBInspectable var height: CGFloat = 0.0

        override func sizeOfTab(_ size: CGSize) -> CGSize {
            var sizeOfTab = super.sizeOfTab(size)
            if height > 0.0 {
                sizeOfTab.height = height
            }
            return sizeOfTab
        }
    }

Assign this class to tab bar

In attribute inspector

It works for me.