繁体   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