user12822558
user12822558

Reputation:

How to add UIView above tabbar?

I have a UIView which I use to make a comments container view: Code:

  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)
}

The problem i'm having is that I run this in the homeVC which is a viewcontroller connected to the parent tabbar controller. And then the tabbarr appears above the container.

How do I make to appear above?

Upvotes: 0

Views: 1690

Answers (1)

KMW
KMW

Reputation: 121

You can try the following:

Method 1. Add commentsContainerView to the tab bar directly (preferred)

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

Method 2. Change the zPosition of tab bar

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

to show again,

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

If you use this method make sure there's no unexpected side effect. e.g. The tab bar elements will still respond to taps.

Method 3. Hide the tab bar

    self.tabBarController?.tabBar.isHidden = true

Upvotes: 3

Related Questions