如何以编程方式替换UITabBarController的UITabBar

Chr*_*fer 3 uitabbarcontroller uitabbar ios

由于以下问题,我需要为我的项目使用UITabBar的子类:为什么页面Push动画Tabbar在iPhone X中向上移动

我不使用情节提要。如何以编程方式完成此操作?

-更新-

我的CustomTabBarController.swift文件现在看起来像这样:

import UIKit

@objc class customTabBarController: UITabBarController {
    override var tabBar: UITabBar {
        return customTabBar
    }
}
Run Code Online (Sandbox Code Playgroud)

我的CustomTabBar.swift文件如下所示:

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但这给了我以下错误:

Cannot convert return expression of type 'customTabBar.Type' to return type 'UITabBar'
Run Code Online (Sandbox Code Playgroud)

Tam*_*gel 7

您应该创建自定义标签栏的实例,并用它覆盖UITabBarController子类中的tabBar属性。

class CustomTabBarController: UITabBarController {
    let customTabBar = CustomTabBar()

    override var tabBar: UITabBar {
        return customTabBar
    }
}    
Run Code Online (Sandbox Code Playgroud)

  • 我认为像覆盖选项卡栏一样不起作用,我使用过但没有任何结果。有谁可以确认一下吗?? (2认同)