将选项卡推入 UITabBarController 内的 ViewController 时不会隐藏

问题描述 投票:0回答:2

出于这个问题的目的,我展示了视图层次结构的精简版本。我的应用程序包含

UITabBarController
作为基础。每个选项卡最顶层的视图控制器是一个导航控制器,并且每个选项卡中都嵌入了视图控制器。

让我们看第一个选项卡。

UITabBarController -> UINavigationController -> UITableViewController -> UIViewController

假设

UITableViewController
实例是某种列表,而
UIViewController
是详细视图。当用户点击列表中的某个项目时,您将进入详细视图。当发生这种情况时,我将
UIViewController
hidesBottomBarWhenPushed
属性设置为
true
,以便当用户处于详细视图中时底部的选项卡栏将隐藏。

我的应用程序接收推送通知。当点击它们时,它应该直接打开到详细视图。我可以让它导航到那里。但问题是底部的标签栏仍然可见!

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
    window = UIWindow(frame: UIScreen.main.bounds)

    let tabBarController = storyboard.instantiateViewController(withIdentifier: "TabBarController") as! TabBarController

    if openingFromPush {
        let firstNavigationController = storyboard.instantiateViewController(withIdentifier: "FirstNavigationController") as! UINavigationController
        let tableViewController = storyboard.instantiateViewController(withIdentifier: "TableViewController") as! TableViewController
        let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
        viewController.hidesBottomBarWhenPushed = true
        firstNavigationController.viewControllers = [tableViewController, viewController]

        tabBarController.viewControllers?[0] = firstNavigationController
        // tabBarController.tabBar.isHidden = true
        window?.rootViewController = tabBarController
    } else {
        window?.rootViewController = tabBarController
    }

    window?.makeKeyAndVisible()
    return true
}

在实例化视图控制器时,我将相同的

hidesBottomBarWhenPushed
属性设置为
true
,但这似乎没有任何效果。我什至尝试像这样直接隐藏标签栏
tabBarController.tabBar.isHidden = true
但这根本没有任何作用。

我不知道如何解决这个问题。任何帮助将不胜感激。

我还附上了一个示例 Xcode 项目here,如果有帮助的话。

ios swift uiviewcontroller uinavigationcontroller uitabbarcontroller
2个回答
5
投票

您可以使用此代码来推送详细视图控制器:

 if openingFromPush {
            let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
            viewController.hidesBottomBarWhenPushed = true
            if let nvc = tabBarController.viewControllers?[0] as? UINavigationController {
                nvc.pushViewController(viewController, animated: false)
            }

            window?.rootViewController = tabBarController
        }

您不需要再次初始化导航视图控制器和表格视图控制器,其已经位于标签栏控制器内


0
投票

在故事板中,选择“按下时隐藏底部栏”

© www.soinside.com 2019 - 2024. All rights reserved.