简体   繁体   English

所有选项卡栏控制器的栏按钮

[英]Bar button for all tab bar controllers

I have three view controllers embedded in UITabBarController. 我在UITabBarController中嵌入了三个视图控制器。 Each view controller is embedded in navigation controller. 每个视图控制器都嵌入在导航控制器中。

The logic of my app is to have navigation right bar button on all tab bar view controllers (ie "Menu" button which should be seen from each view controller). 我的应用程序的逻辑是在所有标签栏视图控制器上都具有导航右栏按钮(即应从每个视图控制器上看到的“菜单”按钮)。 The easiest way to achieve this is to add this button to all navigation controllers separately. 实现此目的的最简单方法是将此按钮分别添加到所有导航控制器。 But I think it is not good solution because then the code for this button must be repeated in each navigation controller. 但我认为这不是一个好的解决方案,因为此按钮的代码必须在每个导航控制器中重复。

Is there any way to have same navigation right button on all controllers of UITabBarController? 有没有办法在UITabBarController的所有控制器上具有相同的导航右键?

(In my app i'm not using Storyboards) (在我的应用中,我没有使用情节提要)

extension UIViewController create a navigation bar Method call anywhere from ViewController in your project.navigation bar will appear with right button menu . 扩展UIViewController创建导航栏在项目的任何地方从ViewController调用方法。导航栏将带有右键按钮菜单。

for example 例如

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

    }

You could create a new UIViewController class, that adds a bar button item to its navigation controller's navigation bar ("Menu" for example). 您可以创建一个新的UIViewController类,该类将栏按钮项添加到其导航控制器的导航栏(例如“菜单”)。 Then your other view controllers could inherit from your new UIViewController class. 然后,其他视图控制器可以从新的UIViewController类继承。 This way, you would have to write the code for adding your bar button only once. 这样,您将只需要编写一次添加栏按钮的代码。

UITabBarController inherits from UIViewController. UITabBarController继承自UIViewController。 Tehnicly You can push UITabBarController in UINavigationController. 您可以在UINavigationController中推送UITabBarController。 Then inside UITabBarController you can setup UIBarButtonItem. 然后在UITabBarController内可以设置UIBarButtonItem。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM