NavigationBar.titleTextAttributes 为零

NavigationBar.titleTextAttributes are nil

我有 2 个视图控制器。现在我想从一个到另一个做一个 present-segue。我在故事板中进行了转场,但在第一个 vc 的源代码中执行了它。将要显示的视图在 viewDidLoad 方法中有这一行:

var font = self.navigationController?.navigationBar.titleTextAttributes![NSAttributedString.Key.font] as! UIFont

在 segue 中加载视图时出现异常,我检查了 titleTextAttributes 是 nil。当我使用 show-segue 时它可以工作,但我不想在我的导航栏中使用这个后退按钮。

我的错误是什么或者有其他解决方案吗?

问题是,当您使用 present 时,您只是在 ViewControllerA 上呈现 ViewControllerB(嵌入 NavigationController), 但由于 ViewControllerB 有不同的 NavigationController 或者可能没有 Navigation Controller,当你出现时,你会 nil 那里

您只需删除该行代码并使用 this

var font = UIFont(name: "Your Font name", size: YOUR_FONT_SIZE)

并像使用 Segue 一样呈现。

希望对您有所帮助