ios 导航控制器标题未显示?

vmpqdwk3  于 2023-01-14  发布在  iOS
关注(0)|答案(7)|浏览(298)

所以当我把我的OptionsViewController作为AppDelegate didFinishLaunchingWithOptions中的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堆栈时位于导航栏的中间,那就太好了!

代码

    • 应用代理. 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
    }
    • 主页视图控制器. 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)
    }
}
    • 选项视图控制器. swift**
class OptionsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource,
    DestinationDelegate, SearchBarCancelDelegate,UISearchBarDelegate,
    CLLocationManagerDelegate {
    override func viewDidLoad() {
        self.title = "Route Options"
    }
e3bfsja2

e3bfsja21#

您需要将navigationItem.title设置为所需的值。如果您需要图像,请设置navigationItem.titleView

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.title = "Your title here"
 }
xwmevbvl

xwmevbvl2#

对于基于标题来到这里的其他人,不要忘记将IB中的ViewController类设置为适当的Swift文件。
这样做之后,我就可以设置标题没有问题使用

self.navigationItem.title = "my title"

self.title = "my title"
gjmwrych

gjmwrych3#

试试看:
HomeViewController中:

let optionsVC = OptionsViewController()
navigationController?.viewControllers = [optionsVC]

在您的OptionsViewController中:

override func viewDidLoad() {
        super.viewDidLoad()

        navigationController?.navigationBar.isTranslucent = false
        navigationItem.title = "Your Title"
    }
d7v8vwbk

d7v8vwbk4#

首先你需要设置UINavigationBar颜色和文本颜色.在didFinishLaunchingWithOptions中试试这个.

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"

}
dz6r00yl

dz6r00yl5#

只需添加下面的行来设置导航项的标题

self.title = "Title 1"
camsedfj

camsedfj6#

使用Objective-C我也遇到了一个问题,它没有显示标题,但转到下一个场景时,标题出现在后退按钮旁边。
我真的不知道为什么,但我解决了这个问题,用Swift而不是Objective-C编写了那个场景的相对视图控制器。
在那之后,它是足够的使用命令:

self.title = "My Title"

所以我可以用if语句或其他方法,通过编程编写我想要的东西。
也许这对使用Objective-C有问题的人有用。

qvsjd97n

qvsjd97n7#

你所需要的就是在viewDidLoad函数中用包含任何可打印符号的字符串初始化navigationItem.title。像""" "这样的字符串将不起作用。如下所示:

override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "."
    }

那么代码navigationItem.title = "Any string"在ViewController中的任何地方都可以很好地工作。

此袋已在iOs 16.2中修复

相关问题