如何在用户 select 选项卡时重置 UITabBarController 的选项卡(或选项卡控制器)?
How to reset tab (or tab controller) of UITabBarController when user select tab?
Swift:我有 UITabBarController,有 8 个选项卡。当用户 select 任何包含更多选项卡的选项卡时,我想通过弹出到 rootView 控制器来重置 selected 选项卡的内容?
这是怎么做到的?
我尝试用以下方法重置导航控制器,它适用于底部可见的选项卡,但不适用于 More
选项卡。
tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)
您可以为您的 tabBar 创建一个 UITabBarController class,并在其中 class 从任何选项卡的 select 覆盖 tabBar didSelect 方法,您可以 popViewController
class TabBarViewController: UITabBarController {
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
let navigationController1 = self.viewControllers![0] as? UINavigationController
navigationController1!.popViewController(animated: false)
let navigationController2 = self.viewControllers![1] as? UINavigationController
navigationController2!.popToRootViewController(animated: false)
let navigationController3 = self.viewControllers![2] as? UINavigationController
navigationController3!.popToRootViewController(animated: false)
let navigationController4 = self.viewControllers![3] as? UINavigationController
navigationController4!.popToRootViewController(animated: false)
}
}
使用 UITabbarController
的 viewControllers
属性。 viewControlers 是 UITabbarController 的 UIViewController 数组。 Exchange/Move 选择标签栏(控制器)时视图控制器的位置。
使用 tabbar(controller) 的委托方法来处理这些操作,例如:
Swift 3
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if var viewcontrollers = tabBarController.viewControllers {
viewcontrollers.remove(at: tabBarController.selectedIndex)
viewcontrollers.insert(viewController, at: 0)
tabBarController.viewControllers = viewControllers
}
}
如果您有权访问已实现的 tabbarController,也可以使用此委托方法。
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
// Write a similar code to exchange/move view controllers using tabbarController instance.
}
一种简洁的方法:
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let index = tabBarController.selectedIndex
if index == NSNotFound || index > 4 {
tabBarController.moreNavigationController.popToRootViewController(animated: false)
return
}
let navController = tabBarController.viewControllers?[tabBarController.selectedIndex] as? UINavigationController
navController?.popToRootViewController(animated: false)
}
为您的 UITabViewContoller 创建一个自定义 class,设置一个委托并将那段代码放在那里。
这对我有用。
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
guard let navigationController = tabBarController.viewControllers?[tabBarController.selectedIndex] as? UINavigationController else {
return
}
navigationController.popToRootViewController(animated: false)
}
要完全支持 More
,请创建自定义 class,如下所示:
class Main_TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
self.customizableViewControllers = [] //remove Edit from More (OPTIONAL)
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if let index = tabBar.items?.firstIndex(of: item) {
if let count = viewControllers?.count, count > 5, index == 4 {
DispatchQueue.main.async {
self.moreNavigationController.popToRootViewController(animated: false)
}
} else if let vcs = viewControllers, let vc = vcs[index] as? UINavigationController {
DispatchQueue.main.async {
vc.popToRootViewController(animated: false)
}
}
}
}
}
Swift:我有 UITabBarController,有 8 个选项卡。当用户 select 任何包含更多选项卡的选项卡时,我想通过弹出到 rootView 控制器来重置 selected 选项卡的内容?
这是怎么做到的?
我尝试用以下方法重置导航控制器,它适用于底部可见的选项卡,但不适用于 More
选项卡。
tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)
您可以为您的 tabBar 创建一个 UITabBarController class,并在其中 class 从任何选项卡的 select 覆盖 tabBar didSelect 方法,您可以 popViewController
class TabBarViewController: UITabBarController {
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
let navigationController1 = self.viewControllers![0] as? UINavigationController
navigationController1!.popViewController(animated: false)
let navigationController2 = self.viewControllers![1] as? UINavigationController
navigationController2!.popToRootViewController(animated: false)
let navigationController3 = self.viewControllers![2] as? UINavigationController
navigationController3!.popToRootViewController(animated: false)
let navigationController4 = self.viewControllers![3] as? UINavigationController
navigationController4!.popToRootViewController(animated: false)
}
}
使用 UITabbarController
的 viewControllers
属性。 viewControlers 是 UITabbarController 的 UIViewController 数组。 Exchange/Move 选择标签栏(控制器)时视图控制器的位置。
使用 tabbar(controller) 的委托方法来处理这些操作,例如:
Swift 3
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if var viewcontrollers = tabBarController.viewControllers {
viewcontrollers.remove(at: tabBarController.selectedIndex)
viewcontrollers.insert(viewController, at: 0)
tabBarController.viewControllers = viewControllers
}
}
如果您有权访问已实现的 tabbarController,也可以使用此委托方法。
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
// Write a similar code to exchange/move view controllers using tabbarController instance.
}
一种简洁的方法:
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let index = tabBarController.selectedIndex
if index == NSNotFound || index > 4 {
tabBarController.moreNavigationController.popToRootViewController(animated: false)
return
}
let navController = tabBarController.viewControllers?[tabBarController.selectedIndex] as? UINavigationController
navController?.popToRootViewController(animated: false)
}
为您的 UITabViewContoller 创建一个自定义 class,设置一个委托并将那段代码放在那里。
这对我有用。
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
guard let navigationController = tabBarController.viewControllers?[tabBarController.selectedIndex] as? UINavigationController else {
return
}
navigationController.popToRootViewController(animated: false)
}
要完全支持 More
,请创建自定义 class,如下所示:
class Main_TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
self.customizableViewControllers = [] //remove Edit from More (OPTIONAL)
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if let index = tabBar.items?.firstIndex(of: item) {
if let count = viewControllers?.count, count > 5, index == 4 {
DispatchQueue.main.async {
self.moreNavigationController.popToRootViewController(animated: false)
}
} else if let vcs = viewControllers, let vc = vcs[index] as? UINavigationController {
DispatchQueue.main.async {
vc.popToRootViewController(animated: false)
}
}
}
}
}