所有標簽欄控制器的欄按鈕

[英]Bar button for all tab bar controllers


I have three view controllers embedded in UITabBarController. Each view controller is embedded in navigation controller.

我在UITabBarController中嵌入了三個視圖控制器。每個視圖控制器都嵌入在導航控制器中。

The logic of my app is to have navigation right bar button on all tab bar view controllers (i.e. "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)

(在我的應用程序中,我不使用故事板)

3 个解决方案

#1


2  

extension UIViewController create a navigation bar Method call anywhere from ViewController in your project.navigation bar will appear with right button menu .

擴展UIViewController創建一個導航欄從project.navigation欄中的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 ")
        }

    }

#2


0  

You could create a new UIViewController class, that adds a bar button item to its navigation controller's navigation bar ("Menu" for example). Then your other view controllers could inherit from your new UIViewController class. This way, you would have to write the code for adding your bar button only once.

您可以創建一個新的UIViewController類,它將一個條形按鈕項添加到其導航控制器的導航欄(例如“菜單”)。然后您的其他視圖控制器可以從您的新UIViewController類繼承。這樣,您只需編寫一次用於添加條形按鈕的代碼。

#3


0  

UITabBarController inherits from UIViewController. Tehnicly You can push UITabBarController in UINavigationController. Then inside UITabBarController you can setup UIBarButtonItem.

UITabBarController繼承自UIViewController。 Tehnicly你可以在UINavigationController中推送UITabBarController。然后在UITabBarController中你可以設置UIBarButtonItem。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/04/23/69e043ff52467cddda8c10fcfd79ec84.html



 
  © 2014-2022 ITdaan.com 联系我们: