高度错误的模态 UINavigationController 栏

modal UINavigationController bar with wrong height

注意:这与 iOS 13.

中使用的新默认模式呈现样式无关

我在呈现模态时遇到一个奇怪的问题 UINavigationController

考虑 UIViewControllerUINavigationController:

当此代码在 iOS 13.0 上运行时:

@IBAction func btntap(_ sender: Any) {

    let errorViewController = UIViewController()
    errorViewController.view.backgroundColor = .blue
    errorViewController.title = "Erro na solicitação"

    let errorNavigation = UINavigationController()

    errorNavigation.navigationBar.barTintColor = UIColor(red: 204/255, green: 0/255, blue: 0/255, alpha: 1.0)

    errorNavigation.navigationBar.tintColor = UIColor.white
    errorNavigation.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]

    errorNavigation.setViewControllers([errorViewController], animated: false)

    errorNavigation.modalPresentationStyle = .automatic

    self.present(errorNavigation, animated: true, completion: nil)
 }

发生这种情况:

注意我们第一次呈现模态屏幕时的错误高度:

我想继续使用 card-like 演示文稿,但我需要在第一个演示文稿中解决这个错误的高度问题。

满足以下要求时会发生这种情况:

  1. 呈现 UIViewControllerUINavigationController

  2. 呈现的 UIViewController 标题中有特殊字符(“ç”、“ã”等)

  3. 现在是animated true

已经尝试了 layoutIfNeeded() 的一些变体,但 none 有效。

如何在第一次展示时以合适的高度展示这个?

只需将 viewcontroller 的标题替换为您自己的标签即可。这是一个黑客解决方案,但将始终有效,您永远不必再考虑它。事实上,我从不调用 viewcontroller 的标题 属性,我只使用标签并将它们设置为 titleView,这样我就可以控制行数、字幕、对齐方式等。

    let errorViewController = UIViewController()
    errorViewController.view.backgroundColor = .blue

    let errorNavigation = UINavigationController()
    let label = UILabel()
    label.text = "Erro na solicitação"
    label.textColor = .white

    errorViewController.navigationItem.titleView = label

    errorNavigation.navigationBar.barTintColor = UIColor(red: 204/255, green: 0/255, blue: 0/255, alpha: 1.0)

    errorNavigation.navigationBar.tintColor = UIColor.white

    errorNavigation.setViewControllers([errorViewController], animated: false)

    errorNavigation.modalPresentationStyle = .automatic

    self.present(errorNavigation, animated: true, completion: nil)

哦,是的,请注意,字体大小应在 17-18 左右,从中等到粗体,以匹配 ios 标题值的默认系统 viewController 如果您希望匹配ios 系统默认值