【问题标题】:Header on view controller with default modalPresentationStyle - iOS 13 [duplicate]具有默认modalPresentationStyle的视图控制器上的标题 - iOS 13 [重复]
【发布时间】:2020-05-11 14:53:25
【问题描述】:

我想在 iOS 13 中使用默认 modalPresentationStyle 向视图控制器添加标题和操作按钮。有关上下文,请参阅警报应用程序中的示例。

我正在展示一个视图控制器,如下所示:

let vc = TestViewController()
self.present(vc, animated: true, completion: nil)

在我的应用程序的其他地方,我使用self.navigationItem.title 来设置标题。这在这种情况下不起作用,因为我正在使用 self.present 来显示新的视图控制器,而不是 self.navigationController?.pushViewController。

如何添加类似于闹钟应用的标题?

【问题讨论】:

  • 您可以将UINavigationBar 添加到您的视图控制器,就像您添加标签、按钮或视图等一样。它不必是UINavigationController 的一部分。
  • 这行得通。我用这个 s/o 帖子作为参考:stackoverflow.com/questions/23859785/…

标签: ios swift ios13


【解决方案1】:

试试这个:

let vc = UINavigationController(rootViewController: TestViewController())
vc.modalPresentationStyle = .fullScreen // if you want to present it in full screen mode
present(vc, animated: true, completion: nil)

在显示的Controller的viewDidLoad中添加标题和带有动作的navigationBarButton:

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "yourButtonTitle", style: .plain, target: self, action: #selector(functionTocall))
navigationItem.title = "yourTitle"

如果我理解正确,这应该可以解决它

【讨论】:

  • 我需要默认显示类型,而不是 .fullScreen。 @DonMag 的评论为我指明了正确的方向
  • 好的,但是如果您不想要全屏演示,您只需删除 vc.modalPresentationStyle 行... :)
猜你喜欢
  • 1970-01-01
  • 2020-01-23
  • 1970-01-01
  • 1970-01-01
  • 2020-02-02
  • 2016-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多