【问题标题】:Bar button for all tab bar controllers所有标签栏控制器的栏按钮
【发布时间】:2017-04-23 16:34:18
【问题描述】:

我在 UITabBarController 中嵌入了三个视图控制器。每个视图控制器都嵌入在导航控制器中。

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

有什么办法可以让UITabBarController的所有控制器都拥有相同的导航右键?

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

【问题讨论】:

    标签: ios swift uinavigationcontroller uitabbarcontroller


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您可以创建一个新的 UIViewController 类,将条形按钮项添加到其导航控制器的导航栏(例如“菜单”)。然后您的其他视图控制器可以从您的新 UIViewController 类继承。这样,您只需编写一次添加条形按钮的代码。

      【讨论】:

        【解决方案3】:

        UITabBarController 继承自 UIViewController。技术上您可以在 UINavigationController 中推送 UITabBarController。然后在 UITabBarController 里面你可以设置 UIBarButtonItem。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-11-22
          • 2023-03-10
          • 2014-04-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多