【问题标题】:Tab bar item as button标签栏项目作为按钮
【发布时间】:2019-07-18 21:26:15
【问题描述】:

我的标签栏中有 5 个标签栏项目,其中 4 个具有导航控制器的 segues,这些导航控制器通向视图控制器。我想让中间的标签栏项目充当一个按钮,这样当我点击它时,我可以控制发生的事情。

目前,我的中间标签栏项目也连接到一个导航控制器,这是不对的,因为现在当我点击标签栏项目时,它会打开一个黑色的导航控制器。如何将中间选项卡栏项转换为按钮,而不是转到导航控制器?

如果我删除导航控制器,它也会从标签栏中删除标签栏项目。

【问题讨论】:

  • 不是我要找的。我不希望在我的标签栏项目上方有一个按钮,我希望其中一个标签栏项目充当按钮而不是导航操作。
  • 您必须创建一个自定义UITabBarController 并访问其委托函数来处理标签栏项目shouldSelect 事件;让您完全控制当有人选择一个项目时该做什么。

标签: ios swift xcode


【解决方案1】:

如果您希望标签栏项充当按钮,您可以继承 UITabBarController 并实现此委托功能:

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {

        // Check if bar item selected is center
        if viewController.tabBarItem.tag == 2 {


            // Do Something Here ...


            // Present View Controller
            guard let navigationController = storyboard?.instantiateViewController(withIdentifier: "NavigationController") as? UINavigationController else { return false }

            present(navigationController, animated: true)

            // Returning false will not open the connected tab bar item view controller you attached to it
            return false

        }

        // Return true to open the connected tab bar item view controller you attached to it (e.x. everything but the center item)
        return true
    }

【讨论】:

    【解决方案2】:

    实现自定义标签栏并像普通按钮一样使用标签栏项目

    1. 向视图控制器 (VC) 添加新的选项卡栏视图
    2. 添加您需要的自定义标签栏项目并为其分配标签编号(在属性检查器上)
    3. 从标签栏视图添加一个委托出口到您的视图控制器(右键单击并拖动到 VC)
    4. 在您的视图控制器上,子类 UITabBarDelegate,像这样
    class ViewController: UIViewController, UITaBarDelegate {}
    
    1. 实现这个委托函数,它应该可以工作
    func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        if item.tag == 1 {
            //Do something
    
        }
        if item.tag == 2 {
            //Do something
        }
        .......
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-19
      • 1970-01-01
      相关资源
      最近更新 更多