当 vc 弹出时,在 UITabBar 上添加子视图显示在 Tab Bar 下

Luc*_*cas 6 uitabbarcontroller uitabbar ios swift

我有一个简单的标签栏项目,我在标签栏上添加了一个图像,起初看起来不错。

使用 hidesBottomBarWhenPushed = true 从 FirstViewController 推送第二个视图。然后我隐藏了添加的按钮,有充分的理由,在视图上而不是作为子视图的标签栏。

一切正常,直到我按下后退按钮,当我取消隐藏视图(或重新创建它)显示在选项卡栏下。

我应该在什么方法中放置 showButton() 方法,以便它再次显示在选项卡栏上?

有小费吗?

CustomTabBarController.swift

class CustomTabBarController: UITabBarController {

    var bigButton: UIImageView!

    func addCenterButton() {
      let image = UIImage(named: "bigButtonBackground")
      bigButton = UIImageView(image: image)
      bigButton.frame = CGRectMake(0.0, 0.0, image!.size.width, image!.size.height);
      bigButton.autoresizingMask = .FlexibleRightMargin | .FlexibleLeftMargin | .FlexibleBottomMargin | .FlexibleTopMargin
      let heightDifference = image!.size.height - tabBar.frame.size.height
      var center = tabBar.center
      center.y = center.y - heightDifference/2.0
      bigButton.center = center

      view.addSubview(bigButton)   
     }

     func hideButton() {
       bigButton.hidden = true   
     }

     func showButton() {
       bigButton.hidden = false
       view.bringSubviewToFront(bigButton)  
     }
}
Run Code Online (Sandbox Code Playgroud)

FirstViewController.swift

class FirstViewController: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    (tabBarController as! CustomTabBarController).addCenterButton()
  }

  override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    (tabBarController as! CustomTabBarController).showButton()
  }

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let viewController = segue.destinationViewController as! UIViewController

    (tabBarController as! CustomTabBarController).hideButton()
    viewController.hidesBottomBarWhenPushed = true
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 1

我正在处理类似的情况并最终进行了子类化UITabBarController。在子类的viewDidLayoutSubviews方法中,调用bringSubviewToFront(view: UIView)并传递选项卡栏后面的视图解决了该问题。确保在UITabBarController的视图上调用此方法。