Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 导航控制器标题未显示?_Ios_Swift_Uiviewcontroller_Uinavigationcontroller - Fatal编程技术网

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
,这都是同样的问题。如果你需要设置导航项的标题,你需要明确告诉导航项。