简体   繁体   English

替换选项卡栏控制器的视图控制器之一

[英]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.

相关问题 后续视图控制器上缺少标签栏控制器 - Tab Bar Controller missing on subsequent View Controllers 如何在Tab栏控制器中使用一个Xib并创建多个视图控制器? [Objective-C] - How to use one Xib and create multiple view controllers in Tab bar controller ? [Objective-C] 检查View Controllers的选项卡栏成员是我的View Controller吗? - Checking View Controllers' tab bar member is my view controller? 在视图控制器->标签栏控制器->导航控制器->视图控制器的层次结构内旋转视图控制器 - Rotating view controllers within a hierarchy of View Controller -> Tab Bar Controller -> Navigation Controller -> View Controller 如何在故事板的标签栏上启动导航控制器的第三个视图控制器 - how to start third view controller of navigation controllers on tab bar in storyboard 标签栏控制器中的两个拆分视图控制器导致崩溃 - Two split view controllers in a tab bar controller causes crash 使用标签栏时在视图控制器之间传递数据 controller - Passing data between view controllers when using a tab bar controller AppDelegate中的iOS初始化选项卡栏控制器视图控制器 - iOS Init Tab Bar Controller View Controllers in AppDelegate 如何在带有Tab Bar Controller的View Controller之间传输浮点值? - How to transfer a float value between View Controllers with Tab Bar Controller? 在视图控制器和标签栏控制器之间传递数据 - Passing data between view controllers and tab bar controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM