繁体   English   中英

Swift如何在Button按钮上显示Tabbar

[英]Swift How to present Tabbar on Button click

在我的项目中,我想在按钮单击时显示Tabbar,现在我已经创建了tabbar,我将身份名称作为“tabbar”,我在下面的图像中显示

在此输入图像描述

所以现在我使用下面的代码来调用Tab栏控制器,但我没有得到它。

 let tabbar: UITabBarController? = (storyboard.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController)

 navigationController?.pushViewController(tabbar, animated: true)

你能告诉我我需要做什么,以及哪些代码对我来说是有用的提示Tabbar控制器。

更多规格:我添加下面的图像,其中一个viewController中有蓝色按钮我想在点击该蓝色按钮时显示标签栏控制器

在此输入图像描述

谢谢。

试试看,看看:
使用Storyboard Segue:使用操作按钮以模态方式连接您的segue。

在此输入图像描述

以编程方式 :使用视图控制器(UIViewController)的self并以模态方式呈现它,就像这样。

if let tabbar = (storyboard.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController) {
    self.present(tabbar, animated: true, completion: nil)
}

结果如下:

在此输入图像描述

使用这个,你没有那里的导航控制器,这就是为什么它不会这样推,相反,你需要使用以下代码:

self.present(tabbar, animated: true, completion: nil)

您应该知道Apple的HIG(人机接口指南)说如果您有一个选项卡式应用程序,它应该是整个应用程序的根级导航。 从人机界面的角度来看,你不应该做你想做的事情。

也就是说,技术上应该是可行的。

我的猜测是你没有导航控制器。

使用调试器检查self.navigationController的值,或添加print语句:

 let tabbar: UITabBarController? = (storyboard.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController)
 print("navigationController = \(navigationController)")
 print("tabbar = \(tabbar)")

 navigationController?.pushViewController(tabbar, animated: true)

如果navigationControllertabbar显示为nil,那就是你的问题。

选择触发按钮单击的viewController,然后选择Editor form form xcode menu-> Embed In-> NavigationController。

然后在按钮操作中写这个

   let tabbar: UITabBarController? = (self.storyboard?.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController)

    self.navigationController?.pushViewController(tabbar!, animated: true)

根据你的图像,我认为你的tabBarController没有后退按钮。 意味着用户无法回头。

如果是这种情况,你可以这样做。

    let tabBar = self.storyboard?.instantiateViewController(withIdentifier: "tabBar")
   let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.window?.rootViewController = tabBar

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM