How can I substitute the UITabBar of UITabBarController programmatically

£可爱£侵袭症+ 提交于 2019-12-02 01:03:36

问题


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
    }
}    


来源:https://stackoverflow.com/questions/47557351/how-can-i-substitute-the-uitabbar-of-uitabbarcontroller-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!