Ios 导航控制器标题未显示?
因此,当我拥有我的Ios 导航控制器标题未显示?,ios,swift,uiviewcontroller,uinavigationcontroller,Ios,Swift,Uiviewcontroller,Uinavigationcontroller,因此,当我拥有我的选项viewcontroller作为AppDelegate中的rootViewController时,使用选项完成启动 let rootVC = OptionsViewController() let navigationController = UINavigationController(rootViewController: rootVC) navigationController.navigationBar.barTintColor =
选项viewcontroller
作为AppDelegate中的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()
…如果我在viewdiload()
中执行此操作,则设置选项viewcontroller
的标题将有效:
但当我将选项ViewController
推到导航堆栈上时,标题不会显示
即,如果我在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
中,我按下我的选项ViewController
,如下所示:
let optionsVC = OptionsViewController()
navigationController?.pushViewController(optionsVC, animated: true)
标题没有显示
我管理标题显示的唯一方法是(在选项ViewController
中)
<>但是它是后退按钮而不是中间按钮,这不是我想要的。
如果有人能告诉我如何设置标题,当它被推到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
}
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)
}
}
class OptionsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource,
DestinationDelegate, SearchBarCancelDelegate,UISearchBarDelegate,
CLLocationManagerDelegate {
override func viewDidLoad() {
self.title = "Route Options"
}
HomeViewController.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
}
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)
}
}
class OptionsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource,
DestinationDelegate, SearchBarCancelDelegate,UISearchBarDelegate,
CLLocationManagerDelegate {
override func viewDidLoad() {
self.title = "Route Options"
}
选项ViewController.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
}
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)
}
}
class OptionsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource,
DestinationDelegate, SearchBarCancelDelegate,UISearchBarDelegate,
CLLocationManagerDelegate {
override func viewDidLoad() {
self.title = "Route Options"
}
您需要将navigationItem.title设置为所需的值。如果需要图像,请设置navigationItem.titleView
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "Your title here"
}
试试看:
在HomeViewController
中:
let optionsVC = OptionsViewController()
navigationController?.viewControllers = [optionsVC]
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.isTranslucent = false
navigationItem.title = "Your Title"
}
在您的选项ViewController
中:
let optionsVC = OptionsViewController()
navigationController?.viewControllers = [optionsVC]
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.isTranslucent = false
navigationItem.title = "Your Title"
}
首先,您需要设置UINavigationBar
颜色和文本颜色。
在didfishlaunchingwithoptions
中尝试此操作
let barAppearance = UINavigationBar.appearance()
barAppearance.barTintColor = UIColor.blue
barAppearance.tintColor = UIColor.white
barAppearance.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
如果要删除backbutton后的字符串
再加上这些
let barItemAppearace = UIBarButtonItem.appearance()
barItemAppearace.setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), for:UIBarMetrics.default)
只需在viewDidLoad()
或
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.title = "Your Title"
}
只需添加下面的行即可设置导航项的标题
self.title = "Title 1"
对于基于标题来到这里的其他人,不要忘记将IB中的ViewController类设置为相应的Swift文件
在这样做之后,我能够使用
self.navigationItem.title = "my title"
或
使用Objective-C时,我还遇到了一个问题,即它没有显示标题,但在进入下一个场景时,标题出现在“后退”按钮旁边
我真的不知道为什么,但我通过在Swift中编程该场景的相对ViewController而不是Objective-C解决了这个问题
之后,就可以使用以下命令:
self.title = "My Title"
因此,我能够使用if语句或其他方法以编程方式编写我想要的内容
也许这对那些在视图控制器而不是导航控制器上有此问题的人来说是有用的,我就是这么做的类选项viewcontroller:UIViewController{override func viewDidLoad(){self.navigationItem.title=“Your title here”}
FYI-self.title=当您从HomeViewController
中按选项ViewController
时,“一些标题”与self.navigationItem.Title=“一些标题”
一样有效,您是否仍在使用self.Title=“路由选项?”"
在选项ViewController
的viewDidLoad
中?@rmaddy Yes我在选项ViewController的viewDidLoad
中设置了标题
,作为测试,注释掉与导航条颜色不一致的行。是的,当我从HomeVC
转换到选项VC,但当我从选项VC
转换到路由跟踪VC
并返回到HomeVC
到选项VC
时,它确实会显示,但这也不起作用。当我从HomeViewController
转换到选项ViewController
时,标题仍然是空的,现在甚至没有显示后退按钮。然而r、 当我从选项viewcontroller
转换到routedtailviewcontroller
时(它被推到导航控制器上
)然后切换回显示标题。这很奇怪,仍然不起作用。当我从HomeViewController
切换到OptionsViewController
时,标题仍然为空。但是,当我从OptionsViewController
切换到RoutedDetailViewController
(推送到NavigationController上)然后切换回显示标题。我已经使用了4个以上的应用程序。请确认您的UIViewController嵌入了uinavigation Controller。我现在添加了UIViewController。出于某种原因,您的代码使其显示,以便我的选项VC
显示在导航栏下。此外,导航栏标题也不会显示直到我从OptionVC
转换到RouteVC
再转换到HomeVC
,然后再转换到OptionVC
,这都是同样的问题。如果你需要设置导航项的标题,你需要明确告诉导航项。