向导航控制器添加按钮

问题描述 投票:0回答:1

我正在从一个没有导航控制器的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)
}

位置TableViewController.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")

}
ios swift uinavigationcontroller uinavigationbar segue
1个回答
0
投票

使用自定义视图创建UIBarButtonItem并将其设置为navigationItem的leftBarButtonItem。

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;

 }
© www.soinside.com 2019 - 2024. All rights reserved.