向导航控制器添加按钮
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了向导航控制器添加按钮相关的知识,希望对你有一定的参考价值。
我正在从一个没有导航控制器的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")
}
答案
使用自定义视图创建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;
}
以上是关于向导航控制器添加按钮的主要内容,如果未能解决你的问题,请参考以下文章