xcode Swift 如何在单击按钮时显示 Tabbar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47793758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:28:34  来源:igfitidea点击:

Swift How to present Tabbar on Button click

iosswiftxcodeuitabbarcontrolleruistoryboardsegue

提问by Bhaumik Joshi

In my project i want to present Tabbar on button click, now i have already created tabbar and i give identity name as "tabbar" that i show you in below image

在我的项目中,我想在单击按钮时显示 Tabbar,现在我已经创建了 tabbar 并且我将身份名称命名为“tabbar”,我在下图中向您展示

enter image description here

在此处输入图片说明

so now i am using below code to call Tab bar controller but i am not getting it.

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

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

 navigationController?.pushViewController(tabbar, animated: true)

can you guys suggest me what i need to do and which code is useful to me to present Tabbar controller.

你们能告诉我我需要做什么以及哪些代码对我展示 Tabbar 控制器有用。

For more specification : i added below image in which there is blue button in one viewController i want to present tab bar controller on click of that blue button

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

enter image description here

在此处输入图片说明

Thank you.

谢谢你。

采纳答案by Krunal

Try this and see:
Using Storyboard Segue:Connect your segue as present modally with your action button.

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

enter image description here

在此处输入图片说明

Programatically: Use selfof view controller (UIViewController) and present it modally, like this.

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

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

Here is result:

这是结果:

enter image description here

在此处输入图片说明

回答by Abhishek Mitra

Use this, you dont have the Navigation controller over there, thats why it won't push that way, instead, you need to use following code:

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

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

回答by Rahul Dasgupta

According to your images I think that your tabBarController has no back button. Means user cannot go back.

根据您的图片,我认为您的 tabBarController 没有后退按钮。意味着用户不能回去。

If that is the case you can do this.

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

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

回答by Duncan C

You should be aware that Apple's HIG (Human Interface Guidelines) say that if you have a tabbed application that should be the root-level navigation for the entire app. you're not supposed to do what you are trying to do from a human interface perspective.

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

That said, it should be technically possible.

也就是说,这在技术上应该是可能的。

My guess is that you don't have a navigation controller.

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

Use the debugger to check the value of self.navigationController, or add a print statement:

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

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

 navigationController?.pushViewController(tabbar, animated: true)

If either navigationControlleror tabbardisplays as nil, that's your problem.

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

回答by Sh_Khan

Select the viewController from which the button click triggered and Select Editor form xcode menu->Embed In->NavigationController.

选择触发按钮单击的 viewController 并选择编辑器表单 xcode menu->Embed In->NavigationController。

then write this in button action

然后在按钮动作中写下这个

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

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