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

标签 ios swift uinavigationcontroller uitabbarcontroller

我在 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/

相关文章:

objective-c - 在 Swift 中传递 @protocol 类型

swift - 属性文本不适用于 Xcode 7 中的自定义字体

iphone - 用 modalview 调用 navigationcontroller?

iOS 工具提示图片

objective-c - 从pdf上下文中获取pdf数据

ios - 如何使用 ReactiveCocoa 4.0 和 MVVM 实现双向绑定(bind)

swift - 为什么我的 Playground 在 NSRange() 上崩溃

ios - 一旦具有适当的大小,如何重新布局 CollectionView 单元格

ios - iPhone 6/6+ 的 UINavigationBar 自定义图像

ios - 从一个 View Controller 切换到另一个 View Controller 的推荐方法是什么?