2017-09-16 134 views
2

我有故事板的設置與類NavigationController: UINavigationController, UIViewControllerTransitioningDelegate與自定義導航欄安裝擴展導航控制器與CustomNavigationBar:UINavigationBar類下面的導航控制器的所有視圖控制器是NavigationController添加右按鈕在NavigationController

代碼
class NavigationController: UINavigationController, UIViewControllerTransitioningDelegate { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 
} 

這裏是爲CustomNavigationBar

class CustomNavigationBar: UINavigationBar { 

    override func awakeFromNib() { 
     var font = UIFont(name: "Montserrat-Light", size: 17)! 
     if "ar" == userLocale() { 
      font = UIFont(name: "DroidSansArabic", size: 17)! 
     } 
     let attribtutes = [ 
      NSFontAttributeName: font, 
      NSForegroundColorAttributeName: UIColor.black, 
      NSKernAttributeName: 5.0 
      ] as [String : Any] 
     UINavigationBar.appearance().titleTextAttributes = attribtutes 
     UINavigationBar.appearance().tintColor = UIColor.black 
     UINavigationBar.appearance().backgroundColor = UIColor.white 

     let image = UIImage(named: "back-btn") 
     UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -66), for: .default) 
     UINavigationBar.appearance().backIndicatorImage = image 
    } 
} 

代碼到目前爲止好,我現在想用一個自定義的視圖中添加合適的欄導航項目。我試着在NavigationController::viewDidLoad()

let customView:UIView = UIView() 
customView.backgroundColor = .green 
customView.frame = CGRect(x: 0, y: 0, width: 100, height: 100) 
let item = UIBarButtonItem(customView: customView) 
self.navigationItem.setRightBarButton(item, animated: false) 

這出於某種原因沒有顯示在導航菜單中添加了下面的代碼。我哪裏錯了?

+0

你需要添加'自我.navigationItem.setRightBarButton(item,animated:false)'在您的基本視圖控制器中的一部分,因爲每個viewController都有它自己的navigationItem,這就是爲什麼您的自定義按鈕沒有顯示。 –

+0

正如您所見,我在NavigationController中添加了這一點。我是否還需要在所有其他ViewController中添加此項? –

+0

如果你使用了一個baseViewController,並在他的viewDidLoad中添加了這個實現,並且讓你的所有其他viewController繼承了你的baseViewController,那麼你只需要添加一次 –

回答

1

你需要在你的基地視圖控制器添加自定義導航按鈕代碼的一部分,因爲每一個的viewController有它自己的navigationItem這就是爲什麼您的自定義按鈕沒有顯示

你需要使用一個BaseViewController類的需求,然後繼承所有的viewController從BaseViewController你只需要添加一次您的導航按鈕自定義代碼,在viewDidLoad

像這樣的事情

class BaseViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     let customView:UIView = UIView() 
     customView.backgroundColor = .green 
     customView.frame = CGRect(x: 0, y: 0, width: 100, height: 100) 
     let item = UIBarButtonItem(customView: customView) 
     self.navigationItem.setRightBarButton(item, animated: false) 
    } 

} 
0

你可以符合你的NavigationController類UINavigationControllerDelegate協議和修改目標控制器在navigationController navigationItem(_ navigationController:UINavigationController的,willShow的viewController:UIViewController中,動畫:布爾)方法:

extension RootNavigationController: UINavigationControllerDelegate { 
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) { 
    if let viewController = viewController as? YOURCONTROLLER { 
     viewController.navigationItem.setRightBarButton(YOURITEM, animated: false) 
    } 
} 
相關問題