如何从视图中删除一个标签栏,使两个标签栏相互重叠?

时间:2019-05-21 09:05:37

标签: ios swift xcode uitabbarcontroller xcode-storyboard

我想从A屏幕上删除黑色标签栏。我试图做下面的代码来筛选,但它删除了白色的代码。

override func viewDidLoad() {
        super.viewDidLoad()
        self.tabBarController?.tabBar.isHidden = true

    }

tab bars enter image description here

2 个答案:

答案 0 :(得分:1)

我认为这对于iOS用户而言不是良好的用户体验,但是无论如何,请尝试以下方法,它将与您一起很好地工作

1-首先构建,请尝试在人造板中构建以下层次结构

enter image description here

请注意,灰色视图Controller不只是UITabBarController,而是UIViewController对象,而是包含一个UITabBar对象

2-您需要给红色视图控制器stroyboard id“ RedViewController ”,并给蓝色视图控制器“ BlueViewController

3-为嵌套的UITabBarItem 标签0 命名为“收藏夹”的第一个UITabBarItem,为第二个名为“更多”的UITabBarItem提供嵌套的标签1

enter image description here 4-最后将以下类添加到具有UIViewController对象的UITabBar中,在我们的示例中,它将是灰色视图控制器

class SecondViewController: UIViewController {

lazy var blueViewController: UIViewController? = {
    self.storyboard?.instantiateViewController(withIdentifier: "BlueViewController")
}()

lazy var redViewController: UIViewController? = {
    self.storyboard?.instantiateViewController(withIdentifier: "RedViewController")
}()

@IBOutlet weak var containerView: UIView!
@IBOutlet weak var nestedTabBar: UITabBar!
override func viewDidLoad() {
    super.viewDidLoad()
    nestedTabBar.delegate = self
    addRedViewController()
    self.nestedTabBar.selectedItem = self.nestedTabBar.items?.first
}

func addBlueViewController() {

    if let redViewController = redViewController {
        redViewController.willMove(toParent: nil)
        redViewController.view.removeFromSuperview()
        redViewController.removeFromParent()
    }

    if let blueViewController = blueViewController {
        addChild(blueViewController)
        blueViewController.view.frame = containerView.frame
        containerView.addSubview(blueViewController.view)
        blueViewController.didMove(toParent: self)
    }
}

func addRedViewController() {

    if let blueViewController = blueViewController {
        blueViewController.willMove(toParent: nil)
        blueViewController.view.removeFromSuperview()
        blueViewController.removeFromParent()
    }

    if let redViewController = redViewController {
        addChild(redViewController)
        redViewController.view.frame = containerView.frame
        containerView.addSubview(redViewController.view)
        redViewController.didMove(toParent: self)
    }  
  }
}

extension SecondViewController: UITabBarDelegate {

func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

    if item.tag == 0 {
        addRedViewController()
    } else {
        addBlueViewController()
    }
  }
}

答案 1 :(得分:0)

tabBarController?.parent?.tabBarController?.tabBar.isHidden = true

尽其所能,但必须确保调用了viewDidLoad。