【问题标题】:Hiding UITabBarController's tabBarItem while being able to show associated view nevertheless隐藏 UITabBarController 的 tabBarItem 同时能够显示关联的视图
【发布时间】:2018-03-07 17:41:14
【问题描述】:

我正在尝试为我的 iPhone“选项卡式应用程序”创建一个导航,该导航将由(显然)UITabBarControllerSWRevealViewController 组成,用于显示侧面菜单。

我的应用程序中的所有视图必须同时显示UITabBarControllerUINavigationBar,但是,出现在左侧菜单中的链接(由SWRevealViewController 处理)不得出现在UITabBarController

我的左侧菜单链接是这样处理的:

import UIKit

class MenuTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.clearsSelectionOnViewWillAppear = false
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let selectedIndex = (indexPath as NSIndexPath).row + 1  // hardcoded for time being
        let tabBarController = revealViewController().frontViewController as! UITabBarController

        let navController = tabBarController.viewControllers![selectedIndex] as! UINavigationController
        navController.popToRootViewController(animated: true)

        tabBarController.selectedIndex = selectedIndex
        revealViewController().pushFrontViewController(tabBarController, animated: false)
    }
}

现在,我尝试删除我不想在UITabBarController 中显示的视图之一的链接,如下所示:

import UIKit

class TabBarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let index = 2  // hardcoded for time being
        viewControllers?.remove(at: index)
    }
}

但如果我现在点击左侧菜单中的关联链接,我会收到 NSRangeException index 2 beyond bounds [0 .. 1] 错误(当然,因为我从 UITabBarController 中删除了特定的 tabBarItem)。

我的问题是:我怎样才能“隐藏”UITabBarController 中的项目,但仍然能够从我的侧菜单中引用它(并打开它)?

更新
我现在的故事板是这样的:

【问题讨论】:

  • 所以...你有一个带有ABC标签的标签栏控制器...当前标签是B...你还有一个菜单列表D EFG...用户在菜单上点击E...应该发生什么?当前标签BE替换?或者A-B-C标签变成A-B-C-E
  • 假设我有 A B C D E 视图。我只想在我的 TabBar 中显示 A B CD E 在我的侧边菜单中。但是,DE 在打开后都应该显示 TabBar。在我的故事板中,所有视图都连接到我的 TabBar 控制器,以便 TabBar 在它们上可见,也许这首先是错误的方法?

标签: ios swift uitabbarcontroller swrevealviewcontroller xcode9


【解决方案1】:

使用“菜单”来操作选项卡可能不是一个好主意 - 这就是 Apple 设计的更多...和编辑...功能的目的。

根据您的整体设计/导航/用户体验流程,两个合理的选择是:

  1. .present 不是替换当前选定的选项卡,而是一个模态视图控制器,用一个“取消”或“保存”或“完成”按钮来.dismiss 它(任何合适的)。

  2. 由于您声明每个 Tab 的 ViewController 都是 NavigationController,因此您可以 .push 将菜单选择的视图控制器添加到当前堆栈上。然后您的界面可以使用标准的“

祝你好运:)

【讨论】:

  • 太棒了!感谢您的澄清!
猜你喜欢
  • 1970-01-01
  • 2019-02-06
  • 1970-01-01
  • 2011-11-19
  • 2019-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多