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

时间:2015-05-16 16:28:27

标签: ios swift uitabbarcontroller uitabbar

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

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

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

1 个答案:

答案 0 :(得分:1)

我正在处理类似情况并最终继承UITabBarController。在子类中' viewDidLayoutSubviews方法,调用bringSubviewToFront(view: UIView)并传入标签栏后面的视图解决了问题。请务必在UITabBarController的视图中调用此方法。