[英]Replace one of the view controllers of tab bar controller
I'm working on an application that uses a UITabBarController and I've assigned 3 ViewControllers to the UITabBarController. 我正在使用UITabBarController的应用程序上工作,并且已将3个ViewController分配给UITabBarController。
I'm building this application programmatically. 我正在以编程方式构建此应用程序。 In AppDelegate I created a UITabBarController.
在AppDelegate中,我创建了一个UITabBarController。 I then create a UINavigationController and set the rootViewController as the UITabBarController.
然后,我创建一个UINavigationController并将rootViewController设置为UITabBarController。
I then set window rootViewController as the UINavigationController 然后将窗口rootViewController设置为UINavigationController
Here's my code from AppDelegate: 这是我来自AppDelegate的代码:
let mainViewcontroller = TabBarController()
let navigationController = UINavigationController(rootViewController: mainViewcontroller)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window!.rootViewController = navigationController
self.window?.makeKeyAndVisible()
Now I created instances of my 3 viewControllers and added them to the viewController list of the UITabBarController. 现在,我创建了3个viewController的实例,并将它们添加到UITabBarController的viewController列表中。 What I want to do now is to display on tap of a button a new different viewController that is not one of the 3 viewControllers that are assigned to the TabBar.
我现在想做的是在按钮的轻击上显示一个新的不同viewController,它不是分配给TabBar的3个viewController之一。 I have been able to achieve this by using the present(ViewController) method:
我已经能够通过使用present(ViewController)方法实现这一点:
newVC = UINavigationController(rootViewController: NewViewController())
self.present(newVC, animated: true, completion: nil)
The problem that I'm having is that I want the viewController to be displayed behind the UITabBarController. 我遇到的问题是我希望viewController显示在UITabBarController的后面。 When I use the present() from above the viewController is displayed over the viewController and the TabBar.
当我从上方使用present()时,viewController会显示在viewController和TabBar上方。 I've tried presenting the viewController by doing self.tabBarController.present() and window.rootViewController.present and I get the same result which is the TabBar is gone.
我尝试通过执行self.tabBarController.present()和window.rootViewController.present来呈现viewController,并且得到的结果是TabBar消失了。 I would appreciate any suggestions.
我将不胜感激任何建议。 Thanks
谢谢
I have created a custom Tab bar which is a subclass of UITabBarController in my application. 我创建了一个自定义标签栏,它是应用程序中UITabBarController的子类。 It has the following functionality for handling a scenario like yours.
它具有以下功能来处理像您这样的情况。
UITabBarController holds an array of its child view controllers. UITabBarController拥有其子视图控制器的数组。 Required (external) view controller is replaced with a currently visible view controller, which is at the selected index of UITabBarController, of that array.
必需的(外部)视图控制器被替换为当前可见的视图控制器,该视图控制器位于该数组的UITabBarController的选定索引处。 Then this updated array is set back to UITabBarController's child view controller array
然后,将此更新后的数组设置回UITabBarController的子视图控制器数组
func setSelectedViewController(_ externalViewController: UIViewController) {
var arrChildViewControllers = self.childViewControllers
if arrChildViewControllers.count > 0 {
let selectedTabIndex = self.selectedIndex
arrChildViewControllers.replaceSubrange(selectedTabIndex...selectedTabIndex, with: [externalViewController])
self.viewControllers = arrChildViewControllers
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.