【发布时间】:2017-04-25 02:15:50
【问题描述】:
所以当我在AppDelegate didFinishLaunchingWithOptions 中有OptionsViewController 作为rootViewController 时...
let rootVC = OptionsViewController()
let navigationController = UINavigationController(rootViewController: rootVC)
navigationController.navigationBar.barTintColor = .white
navigationController.navigationBar.isTranslucent = false
navigationController.navigationBar.tintColor = .black
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window!.rootViewController = navigationController
self.window?.makeKeyAndVisible()
...如果我在 viewDidLoad() 中执行此操作,则设置 OptionViewController 的标题有效:
title = "Route Options"
但是当我将OptionsViewController 推送到导航堆栈时,标题没有显示出来。
即如果我以与AppDelegate 中的rootViewController 不同的视图开始:
let rootVC = HomeViewController()
let navigationController = UINavigationController(rootViewController: rootVC)
navigationController.navigationBar.barTintColor = .white
navigationController.navigationBar.isTranslucent = false
navigationController.navigationBar.tintColor = .black
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window!.rootViewController = navigationController
self.window?.makeKeyAndVisible()
在HomeViewController 中,我像这样推送我的OptionViewController:
let optionsVC = OptionsViewController()
navigationController?.pushViewController(optionsVC, animated: true)
标题不显示!
我设法让标题显示的唯一方法是(在OptionViewController)
navigationController?.navigationBar.topItem?.title = "Route Options"
但它显示为后退按钮而不是在中间,这不是我想要的。
如果有人能告诉我如何设置标题,使其在被推入 navigationController 堆栈时位于导航栏的中间,那就太好了!
代码
AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let rootVC = HomeViewController()
let navigationController = UINavigationController(rootViewController: rootVC)
let barAppearance = UINavigationBar.appearance()
barAppearance.barTintColor = UIColor.blue
barAppearance.tintColor = UIColor.white
barAppearance.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window!.rootViewController = navigationController
self.window?.makeKeyAndVisible()
return true
}
HomeViewController.swift
class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, DestinationDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let optionsVC = OptionsViewController()
self.definesPresentationContext = false //else going to try and present optionVC on homeVC when in optionVC
navigationController?.pushViewController(optionsVC, animated: true)
}
tableView.deselectRow(at: indexPath, animated: true)
}
}
OptionsViewController.swift
class OptionsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource,
DestinationDelegate, SearchBarCancelDelegate,UISearchBarDelegate,
CLLocationManagerDelegate {
override func viewDidLoad() {
self.title = "Route Options"
}
【问题讨论】:
-
当你从
HomeViewController推送OptionsViewController时,你还在viewDidLoad的viewDidLoad中使用self.title = "Route Options"吗? -
@rmaddy 是的,我在
viewDidLoad的OptionsViewController中设置了title -
作为测试,注释掉与导航栏颜色混淆的行。
-
是的,当我从
HomeVC转换到OptionsVC时,它仍然不起作用,但是当我从OptionsVC转换到RouteDetailVC并返回到HomeVC到 @ 时,它确实出现了987654359@
标签: ios swift uiviewcontroller uinavigationcontroller