【问题标题】:Programmatically Created UINavigationController with Large Title Falls Under UIStatusBar以编程方式创建的 UINavigationController 大标题落在 UIStatusBar 下
【发布时间】:2017-10-13 10:06:47
【问题描述】:

我正在以编程方式创建UINavigationController

navigationController = UINavigationController(rootViewController: modeSelectVC!)
navigationController?.delegate = self

window.rootViewController = navigationController
window.makeKeyAndVisible()

然后我像这样自定义我的UINavigationBar 外观:

navigationBarAppereance.barTintColor = UIColor.ColorPalette.bostonUniversityRed
navigationBarAppereance.tintColor = UIColor.ColorPalette.eerieBlack
navigationBarAppereance.barStyle = .black
navigationBarAppereance.isTranslucent = true
navigationBarAppereance.prefersLargeTitles = true
navigationBarAppereance.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.ColorPalette.babyPowder]

配置的UINavigationBar隐藏在与状态栏一起提供的rootViewController中,但是当rootViewController推送另一个视图控制器时,状态栏和导航栏都变得可见,如下所示:

结果正如我编程的那样,但正如您在上面看到的那样,back 按钮未对齐。如果我放开大标题偏好,那么返回按钮像往常一样正确对齐,但是为什么在使用大标题时它会错位。

【问题讨论】:

    标签: swift uinavigationcontroller uinavigationbar


    【解决方案1】:

    这个here有一个很好的答案:

    您应该将导航栏的顶部固定到状态栏的底部,并使用 UIBarPositioningDelegate 机制将其位置设置为 .topAttached,这将使其正确地伸展到状态栏下方。

    【讨论】:

    • 我添加了这个:func position(for bar: UIBarPositioning) -> UIBarPosition { return .topAttached } 但遗憾的是问题仍然存在。
    • @CanSürmeli 试试navigationController.navigationBar.clipsToBounds = false?
    • 很遗憾,再也没有了。
    • @CanSürmeli navigationController?.navigationBar.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0) 怎么样?
    • 我收到错误:Value of type UINavigationBar` 没有成员`anchor`。坦率地说,我不认为或不想设置/修改 UINavigationBar 的位置,因为它会自动对齐。否则,我觉得它可能会破坏某些东西。
    【解决方案2】:

    我有点解决了这个问题。一旦我在初始视图控制器上启用了状态栏,之后,无论呈现什么,都会正确显示导航栏和状态栏。

    我仍然不知道是什么导致了问题(如果是我的实现错误或文档不佳),但处理 UINavigationBar 可能会很棘手!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      相关资源
      最近更新 更多