xcode 如何将navigationBar放在前面

tkclm6bt  于 2023-08-07  发布在  其他
关注(0)|答案(4)|浏览(102)

我在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()
}

字符串
此外,附上了布局的截图我错过了什么?感谢您的帮助.

9fkzdhlc

9fkzdhlc1#

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

cfh9epnr

cfh9epnr2#

添加此

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

字符串

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

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

ybzsozfc

ybzsozfc3#

我终于修好了。这个问题与我的这部分代码无关。它与另一个控制器相关,该控制器是一个CustomTabController并包含navigationBar。我不能用这个代码覆盖它。因此,我将navigationBar部分移到CustomBarController之外,解决了这个问题。这个答案是非常具体的我的项目,只是分享它,让读者知道,它是固定在一个不同的方式。

bvjveswy

bvjveswy4#

我也遇到了同样的问题,在我的例子中,我通过将以下代码移动到viewWillAppear来修复它

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

字符串
然后添加以下代码:

override func viewDidAppear(_ animated: Bool) {
   super.viewDidAppear(animated)
   if let navigationController = navigationController {
       navigationController.view.bringSubviewToFront(navigationController.navigationBar)
    }
}

相关问题