如何在Swift 3中隐藏TabBarController上的自定义按钮?

3
我有一个带有自定义按钮的UITabBarController。但是如果我设置hidesBottomBarWhenPushed = true,则会出现奇怪的行为。 我在Swift 3中通过编程方式创建了UITabBarController。 这是我的代码来创建自定义中间按钮:
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")
}

如何解决这个问题?我希望中间按钮保持在底部栏中。谢谢提前。
1个回答

4
我能够通过以下方法解决它:
在类中实例化菜单按钮:
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中使用它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接