gpt4 book ai didi

ios - 所有选项卡栏 Controller 的栏按钮

转载 作者:搜寻专家 更新时间:2023-11-01 07:13:35 24 4
gpt4 key购买 nike

我在 UITabBarController 中嵌入了三个 View Controller 。每个 View Controller 都嵌入在导航 Controller 中。

我的应用程序的逻辑是在所有选项卡栏 View Controller 上都有导航右栏按钮(即应该从每个 View Controller 中看到的“菜单”按钮)。实现这一点的最简单方法是将此按钮分别添加到所有导航 Controller 。但我认为这不是好的解决方案,因为此按钮的代码必须在每个导航 Controller 中重复。

有没有办法让 UITabBarController 的所有 Controller 都具有相同的导航右键?

(在我的应用中我没有使用 Storyboard)

最佳答案

extension UIViewController 创建一个导航栏 从项目中的 ViewController 的任何地方调用方法。导航栏将与右键菜单一起出现。

例如

    import UIKit

extension UIViewController {

func setupNavigationBar(title: String) {
// back button without title
//self.navigationController?.navigationBar.topItem?.title = ""

//back button color
//self.navigationController?.navigationBar.tintColor = UIColor.white

//set titile
self.navigationItem.title = title

//set text color & font size
//self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.init(red: 251/255, green: 251/255, blue: 251/255, alpha: 1) , NSFontAttributeName:UIFont.systemFont(ofSize: 19)]

//set background color without gradian effect
//self.navigationController?.navigationBar.barTintColor = UIColor.init(red: 134/255, green: 145/255, blue: 152/255, alpha: 1)

//show right button
let rightButton = UIBarButtonItem(image: #imageLiteral(resourceName: "Menu"), style: .plain, target: self, action: #selector(menu))

//right Bar Button Item tint color
//self.navigationItem.rightBarButtonItem?.tintColor = UIColor.init(red: 219/255, green: 219/255, blue: 219/255, alpha: 1)

//show the Menu button item
self.navigationItem.rightBarButtonItem = rightButton

//show bar button item tint color
//self.navigationItem.rightBarButtonItem?.tintColor = UIColor.init(red: 219/255, green: 219/255, blue: 219/255, alpha: 1)

}

func menu(){
print("showSlideOutMane fire ")
}

}

关于ios - 所有选项卡栏 Controller 的栏按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43573730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com