我有一个带有3个标签栏按钮的标签栏控制器。目前它看起来像这样:
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]
}
}
现在,使用上面的代码,所有视图控制器都位于CustomTabBarController
我想要中间的标签栏按钮secondTabBarNavigationController
来呈现一个视图控制器,特别是UIImagePickerController
,供用户选择图像,类似于Instagram。
如何实现这一目标?我没有使用故事板
答案 0 :(得分:6)
只需实现tabBar控制器的func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
委托方法。
按下所需选项卡后,显示控制器并返回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
}
}