How can I substitute the UITabBar of UITabBarContr

2020-03-26 06:06发布

问题:

I need to use a subclass of the UITabBar for my project because of the following problem Why page Push animation Tabbar moving up in the iPhone X.

I do not use storyboards. How can this be done programmatically?

-- update --

My CustomTabBarController.swift file now looks like this:

import UIKit

@objc class customTabBarController: UITabBarController {
    override var tabBar: UITabBar {
        return customTabBar
    }
}

And my CustomTabBar.swift file looks like this:

import UIKit

class customTabBar: UITabBar {

    override var frame: CGRect {
        get {
            return super.frame
        }
        set {
            var tmp = newValue
            if let superview = superview, tmp.maxY !=
                superview.frame.height {
                tmp.origin.y = superview.frame.height - tmp.height
            }

            super.frame = tmp
        }
    }
}

But this gives me the following error:

Cannot convert return expression of type 'customTabBar.Type' to return type 'UITabBar'

回答1:

You should create an instance of your custom tab bar and override the tabBar property inside your UITabBarController subclass with it.

class CustomTabBarController: UITabBarController {
    let customTabBar = CustomTabBar()

    override var tabBar: UITabBar {
        return customTabBar
    }
}