2017-04-08 98 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