簡體   English   中英

如何在標簽欄上方添加 UIView?

[英]How to add UIView above tabbar?

我有一個 UIView 用於制作評論容器視圖:代碼:

  func setupCommentsContainerView() {
    commentsContainerView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(commentsContainerView)

    NSLayoutConstraint.activate([
        commentsContainerView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
        commentsContainerView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
        commentsContainerView.topAnchor.constraint(equalTo: view.topAnchor, constant: 200),
        commentsContainerView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0),
    ])

    // add child view controller view to container

    let controller = storyboard!.instantiateViewController(withIdentifier: "commentsVC")
    addChild(controller)
    controller.view.translatesAutoresizingMaskIntoConstraints = false
    commentsContainerView.addSubview(controller.view)

    NSLayoutConstraint.activate([
        controller.view.leadingAnchor.constraint(equalTo: commentsContainerView.leadingAnchor),
        controller.view.trailingAnchor.constraint(equalTo: commentsContainerView.trailingAnchor),
        controller.view.topAnchor.constraint(equalTo: commentsContainerView.topAnchor),
        controller.view.bottomAnchor.constraint(equalTo: commentsContainerView.bottomAnchor)
    ])

    controller.didMove(toParent: self)
}

我遇到的問題是我在 homeVC 中運行它,這是一個連接到父標簽欄 controller 的視圖控制器。 然后 tabbarr 出現在容器上方。

我如何使出現在上面?

您可以嘗試以下方法:

方法一、直接在標簽欄添加commentsContainerView(首選)

    if let tab = tabBarController {
    tab.view.addSubview(commentsContainerView)
    //Your code
    }

方法2.改變標簽欄的zPosition

    self.tabBarController?.tabBar.layer.zPosition = -1

再次展示,

    self.tabBarController?.tabBar.layer.zPosition = 0

如果您使用此方法,請確保沒有意外的副作用。 例如,標簽欄元素仍然會響應點擊。

方法3.隱藏標簽欄

    self.tabBarController?.tabBar.isHidden = true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM