2017-04-08 45 views
3

我有一个UITabBarController与TabBar中间的自定义按钮。但是如果我设置hidesBottomBarWhenPushed = true,我会发现一个奇怪的行为。如何在Swift 3中隐藏TabBarController上的自定义按钮?

func setupMiddleButton() { 
     let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 48, height: 48)) 

     var menuButtonFrame = menuButton.frame 
     menuButtonFrame.origin.y = view.bounds.height - menuButtonFrame.height 
     menuButtonFrame.origin.x = view.bounds.width/2 - menuButtonFrame.size.width/2 
     menuButton.frame = menuButtonFrame 

     menuButton.layer.cornerRadius = menuButtonFrame.height/2 
     view.addSubview(menuButton) 

     menuButton.setImage(UIImage(named: "updatemoment"), for: .normal) 
     menuButton.addTarget(self, action: #selector(menuButtonAction), for: .touchUpInside) 

     view.layoutIfNeeded() 
    } 

func menuButtonAction() { 
    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
    let vc: UINavigationController = storyboard.instantiateViewController(withIdentifier: "NewPostID") as! UINavigationController 

    self.present(vc, animated: true, completion: nil) 
    print("segue success") 
} 

如何解决这个问题:

enter image description here

我编程斯威夫特3.

这里是我的代码来创建自定义中间按钮创建UITabBarController?我想要中间的按钮留在BottomBar

在此先感谢。

回答

3

我能够解决它:

实例化类中的菜单按钮:

let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 64, height: 64)) 

增加两个功能在同一个控制器(TabBarController):

func hideTabBar() { 
    self.tabBar.isHidden = true 
    self.menuButton.isHidden = true 
} 

func showTabBar() { 
    self.tabBar.isHidden = false 
    self.menuButton.isHidden = false 
} 

而且那么无论何时需要隐藏或显示tabBar,请使用:

let tabBar = self.tabBarController as! InitialViewController 
tabBar.showTabBar() 

我目前在viewWillAppear和viewWillDisappear中使用它在一些控制器中。

+0

工程就像一个魅力。非常感谢你。 – Badrinath

相关问题