ios - 如何从标签栏按钮呈现 View Controller

标签 ios swift

我有一个带有 3 个标签栏按钮的标签栏 Controller 。目前它看起来像这样:

class CustomTabBarController: UITabBarController {

override func viewDidLoad() {
    super.viewDidLoad()

    let firstTabBarController = FirstController()
    let firstTabBarNavigationController = UINavigationController(rootViewController: firstTabBarController)
    firstTabBarNavigationController.tabBarItem.title = "First Tab"

    let secondTabBarController = SecondController()
    let secondTabBarNavigationController = UINavigationController(rootViewController: secondTabBarController)
    secondTabBarController.tabBarItem.title = "Second Tab"

    let thirdTabBarController = ThirdController()
    let thirdTabBarNavigationController = UINavigationController(rootViewController: thirdTabBarController)
    thirdTabBarNavigationController.tabBarItem.title = "Third Tab"

    viewControllers = [firstTabBarNavigationController, secondTabBarNavigationController, thirdTabBarNavigationController]

}
}

现在,使用上面的代码,所有 View Controller 都位于 CustomTabBarController

我希望中间的标签栏按钮 secondTabBarNavigationController 呈现一个 View Controller ,特别是 UIImagePickerController 供用户选择图像,类似于 Instagram。

如何实现?我没有使用 Storyboard

最佳答案

只需实现 tabBar Controller 的 func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool 委托(delegate)方法。

当按下所需的选项卡时,显示一个 Controller 并返回 false,否则返回 true

我还会将一个空的 UIViewController 实例关联到选项卡。

例如

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    if (tab for this controller is equal to the expected tab) {
        // present you controller
        return false
    } else {
        return true
    }
}

关于ios - 如何从标签栏按钮呈现 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41280168/

相关文章:

ios - 线程 1 : EXC_BAD_INSTRUCTION when trying to write a message JSQMessagesViewController

swift - 按名称获取类(class)

ios - 在 swift 3 中单击按钮从 Firebase 中删除 "posts"

ios - 单击 UITextField 时如何显示联系人?

ios - 单元测试(猕猴桃): Property is always nil in test after loadView and viewDidLoad are called

ios - 图像阵列中的视频未播放

iphone - 使用 8 个图像在 UIImageView 周围创建边框

ios - 启动屏幕上用户定义的运行时属性

swift - 我可以直接使用类型元组定义函数头吗?

swift - 在 iOS 13 中将 UIImagePickerController 导航栏设置为不透明