【问题标题】:modal UINavigationController bar with wrong height高度错误的模态 UINavigationController 栏
【发布时间】:2019-10-29 05:48:57
【问题描述】:

注意:这与 iOS 13 中使用的新默认模态演示样式无关。

我有一个奇怪的问题,显示模态 UINavigationController

考虑在UINavigationController 中的UIViewController

当此代码在 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)
 }

发生这种情况:

当我们第一次展示模态屏幕时注意错误的高度:

我想继续使用卡片式演示,但我需要在第一次演示时解决这个错误的高度问题。

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

  1. 呈现UIViewControllerUINavigationController

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

  3. 现在是animated true

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

如何在第一个礼物上以正确的高度展示这个?

【问题讨论】:

  • 对不起,我搜索了很多,没有发现任何类似的问题。问题是第一个礼物的导航栏高度,而不是演示文稿表单。如果标题具有误导性,您能否推荐一个更好的,以便我编辑问题?
  • 这可能无济于事,只能将let errorNavigation = UINavigationController() 更改为let errorNavigation = UINavigationController(rootViewController: errorViewController) 并删除errorNavigation.setViewControllers([errorViewController], animated: false) 行。
  • 不,同样的问题,但感谢您的尝试
  • 您可以尝试从self.navigationController 而不是self 进行演示吗?我之前从子控制器演示时遇到过问题。

标签: ios swift ios13


【解决方案1】:

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

    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 系统默认值

【讨论】:

  • 这感觉有点hacky,但解决了问题。我会用它。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2015-06-01
  • 2012-07-05
  • 2021-04-26
  • 1970-01-01
  • 1970-01-01
  • 2018-08-27
  • 2017-09-09
  • 1970-01-01
相关资源
最近更新 更多