gpt4 book ai didi

ios - 将按钮添加到导航 Controller

转载 作者:行者123 更新时间:2023-11-28 15:09:38 25 4
gpt4 key购买 nike

我正在从没有导航 Controller 的 ViewController 推送到有导航 Controller 的 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")

}

最佳答案

使用自定义 View 创建一个 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;

}

关于ios - 将按钮添加到导航 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47876223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com