【问题标题】:Tabbar won't hide when pushed into a ViewController inside a UITabBarController将 Tabbar 推入 UITabBarController 内的 ViewController 时不会隐藏
【发布时间】:2017-06-14 22:56:25
【问题描述】:

出于这个问题的目的,我展示了我的视图层次结构的精简版本。我的应用程序包含一个 UITabBarController 作为基础。每个选项卡最顶层的视图控制器是一个导航控制器,并且每个控制器都嵌入了视图控制器。

让我们选择第一个标签。

UITabBarController -> UINavigationController -> UITableViewController -> UIViewController

假设UITableViewController 实例是某种列表,UIViewController 是详细视图。当用户点击列表中的某个项目时,它会将您带到详细视图。当发生这种情况时,我将UIViewControllerhidesBottomBarWhenPushed 属性设置为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


    【解决方案1】:

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

     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
            }
    

    您不需要再次初始化导航视图控制器和表格视图控制器,因为它已经在标签栏控制器中

    【讨论】:

    • 顾名思义,视图控制器需要推送到导航控制器内部
    猜你喜欢
    • 2015-09-26
    • 1970-01-01
    • 2017-01-28
    • 2016-10-07
    • 1970-01-01
    • 2011-04-20
    • 2023-03-05
    • 2019-10-27
    • 2012-01-31
    相关资源
    最近更新 更多