2017-12-18 22 views
0

私はViewControllerからナビゲーションコントローラなしでViewControllerにプッシュしています。ナビゲーションコントローラにボタンを追加

NavigationControllerをプログラムで作成していますが、ナビゲーションバーにボタンを追加するのが難しいです。

func goToLocation() { 
    let locationTableVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "locationProfile") as! LocationTableViewController 
    locationTableVC.documentId = selectedDocumentId! 

    let navigationController = UINavigationController(rootViewController: locationTableVC) 

    self.present(navigationController, animated: true, completion: nil) 
} 

LocationTableViewController.swift

// MARK: - View Will Appear 
override public func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
    UIApplication.shared.statusBarStyle = .lightContent 

    // Make Nav Bar Translucent and Set title font/color 
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) 
    self.navigationController?.navigationBar.shadowImage = UIImage() 
    self.navigationController?.navigationBar.isTranslucent = true 
    self.navigationController?.view.backgroundColor = .clear 
    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 20, weight: .semibold)] 
    self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "back-arrow-white") 

} 
+1

どのボタンを追加しますか? –

+0

カスタム戻るボタン – pmanning

+0

'self.navigationItem.leftBarButtonItem = UIBarButtonItem(.....)'または 'self.navigationItem.backBarButtonItem = UIBarButtonItem(....)'注意: '' viewWillAppear '..これは' viewDidLoad'で行うことができます。 'navigationBar'プロパティにのみアクセスするには' viewWillAppear'かコンストラクタが必要です。代わりにすべての戻るボタンをカスタムイメージにしたい場合は、 'UIAppearance'プロキシを使用してください。 – Brandon

答えて

0

&がnavigationItemのleftBarButtonItemに設定したカスタムビューを使用してUIBarButtonItemを作成します。

override func viewDidLoad() { 

    super.viewDidLoad() 
    let backButton = UIButton(type: .custom) 
    backButton.frame = CGRect(x:0,y:0,width: 45, height:45) 
    backButton.setImage(UIImage(named: "back-arrow-white"), for: .normal) 
    let backBarButtonItem = UIBarButtonItem.init(customView: backButton) 
    self.navigationItem.leftBarButtonItem = backBarButtonItem; 

} 
関連する問題