swift 在UINavigationBar中看不到rightBarButtonItem?

wn9m85ua  于 2022-12-03  发布在  Swift
关注(0)|答案(2)|浏览(371)

我的UIViewControllers中有一个内嵌在UITabBarNavigationController中的自订UINavigationBarController。但是,我无法在navigationBar中看到rightBarButtonItem。
应用程序委派:

func application(_ application: UIApplication, 
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?)
-> Bool {
    /**Setup UITabBarController*/
    let window = UIWindow.init()
    self.window = window
    window.makeKeyAndVisible()
    window.rootViewController = CustomTabBarController()

    return true
}

class CustomTabBarController: UITabBarController
{
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        // Setup viewControllers
        let navController = CustomNavigationController()
        navController.tabBarItem.title = "Home"
        viewControllers = [navController]      
    }
}

class CustomNavigationController: UINavigationController
{
    override func viewDidLoad()
    {
        navigationBar.barTintColor = UIColor.app093DA5
        let rightBarButton = UIButton.init(type: .system)
        rightBarButton.setImage(UIImage.init(named: "menuWhite"), for: .normal)
        rightBarButton.frame = CGRect.init(x: 0, y: 0, width: 34, height: 34)
        navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: rightBarButton)
    }

    @objc private func rightBarButtonAction(){}
}
rks48beu

rks48beu1#

您正尝试在UINavigationController的导航栏上添加按钮,但它无法正常工作。
UINavigationController需要UIViewController才能在屏幕上显示UIButton
您可以在此处阅读更多信息:

您必须添加一个UIViewController来显示该按钮:

class CustomTabBarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        // Setup viewControllers
        let navController = CustomNavigationController(rootViewController: ViewController())
        navController.tabBarItem.title = "Home"
        viewControllers = [navController]      
    }
}

class CustomNavigationController: UINavigationController {

    override func viewDidLoad() {
        navigationBar.barTintColor = UIColor.app093DA5
    }

}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let rightBarButton = UIButton(type: .system)
        rightBarButton.setImage(UIImage(named: "menuWhite"), for: .normal)
        rightBarButton.frame = CGRect.init(x: 0, y: 0, width: 34, height: 34)
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightBarButton)
    }
}

如果有用的话试试这个。

编辑:

我使用了上面的相同代码,除了一个小的变化:

class CustomNavigationController: UINavigationController {

    override func viewDidLoad() {
        navigationBar.barTintColor = UIColor.gray
    }

}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        let rightBarButton = UIButton(type: .system)
        //rightBarButton.setImage(UIImage(named: "menuWhite"), for: .normal)
        rightBarButton.setTitle("Home", for: .normal)
        rightBarButton.setTitleColor(UIColor.black, for: .normal)
        rightBarButton.frame = CGRect.init(x: 0, y: 0, width: 34, height: 34)
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightBarButton)
    }
}

输出为:

nwnhqdif

nwnhqdif2#

在某些情况下,导航栏的使用将通过寻址navigationBar及其topItem(而不是navigationItem)来实现,如下所示:

navigationController?.navigationBar.topItem?.rightBarButtonItem = barButton

这将在项目中工作,当有人使用主导航对象为整个应用程序,但最初使用的是navigationBar

相关问题