【问题标题】:How to hide custom button on TabBarController in Swift 3?如何在 Swift 3 中隐藏 TabBarController 上的自定义按钮?
【发布时间】:2017-09-03 21:22:38
【问题描述】:

我在 TabBar 中间有一个带有自定义按钮的 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")
}

如何解决这个问题?我希望中间按钮留在BottomBar

提前致谢。

【问题讨论】:

    标签: ios swift3 uitabbarcontroller


    【解决方案1】:

    我能够通过以下方式修复它:

    在类中实例化菜单按钮:

    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 中使用它。

    【讨论】:

      猜你喜欢
      • 2017-05-04
      • 1970-01-01
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-04
      相关资源
      最近更新 更多