【问题标题】:How to bring the navigationBar to the front如何将导航栏置于最前面
【发布时间】:2019-10-03 07:58:52
【问题描述】:

我在 swift 4 中实现了一个自定义的后退按钮,但该按钮不显示。我可以在它所在的层中看到它,但它在后面,不能把它带到前面。 这是我在 viewDidload 中的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor.white
    collectionView?.translatesAutoresizingMaskIntoConstraints = false

    collectionView?.contentInset = UIEdgeInsets(top: 8, left: 0, bottom: 58, right: 0)
    collectionView?.scrollIndicatorInsets = UIEdgeInsets(top: 0, left: 0, bottom: 50, right: 0)
    collectionView?.alwaysBounceVertical = true
    collectionView?.backgroundColor = UIColor(patternImage: UIImage(named: "chat-bg")!)
    collectionView?.register(ChatMessageCell.self, forCellWithReuseIdentifier: cellId)

    collectionView?.keyboardDismissMode = .interactive


    let username = Auth.auth().currentUser?.displayName
    let backbutton = UIButton(type: .custom)
    backbutton.translatesAutoresizingMaskIntoConstraints = false
    backbutton.setImage(UIImage(named: "back"), for: .normal) // Image can be downloaded from here below link
    backbutton.setTitle(username, for: .normal)
    backbutton.setTitleColor(backbutton.tintColor, for: .normal) // You can change the TitleColor
    backbutton.addTarget(self, action: #selector(backAction), for: .touchUpInside)

    self.navigationController?.navigationItem.backBarButtonItem = UIBarButtonItem(customView: backbutton)

    self.navigationController?.navigationBar.addSubview(backbutton)
    setupInputComponents()
    setupKeyboardObservers()
}

另外,附上布局的截图 我错过了什么?任何帮助表示赞赏。

【问题讨论】:

  • 显示分配给collectionView的约束。

标签: ios swift xcode user-interface uinavigationbar


【解决方案1】:

您能否检查一下集合的顶部约束,它需要来自超级视图或安全区域,而不是来自主视图的顶部。基本上,集合视图隐藏了您的导航栏。如果您能告诉我集合视图约束,它将有助于轻松解决您的问题。

【讨论】:

  • 我没有上面提到的这个collectionViewController的任何其他限制。
  • 让 yourBackImage = UIImage(named: "back_button_image") self.navigationController?.navigationBar.backIndicatorImage = yourBackImage self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = yourBackImage self.navigationController?.navigationBar.backItem?.title = "自定义" 你可以试试这个代码吗?
  • 会不会是因为我使用了present而不是pushViewController?我改变了它,现在看到所有 viewControllers 上的返回按钮:D
  • 是的,没错。如果您想呈现控制器而不是需要在 uinavigation 控制器中添加视图控制器而不是当前导航控制器,但它不会显示显示按钮的后退按钮,您需要设置导航栏左栏按钮项。否则只需使用 calayeranimation 更改动画并以当前样式推送控制器。
【解决方案2】:

添加这个

UIApplication.shared.keyWindow!.bringSubviewToFront(self.navigationController?.navigationBar)

self.navigationController?.navigationBar.layer.zPosition = 1


self.navigationController?.navigationBar.alpha = 1.0
self.navigationController?.navigationBar.translucent = false

【讨论】:

  • 编辑了我的答案。立即查看
【解决方案3】:

我终于修好了。该问题与我的这部分代码无关。它与另一个控制器相关,该控制器是 CustomTabController 并包含导航栏。我无法通过此代码覆盖它。因此,我将 navigationBar 部分移到了 CustomBarController 之外,从而解决了这个问题。这个答案非常针对我的项目,只是为了让读者知道它以不同的方式修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多