双击 UITabBarController 时防止自动 popToRootViewController 的方法如下:
下面是示例代码:
class CustomTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap))
tapGestureRecognizer.numberOfTapsRequired = 2
view.addGestureRecognizer(tapGestureRecognizer)
}
@objc func handleTap(sender: UITapGestureRecognizer) {
guard let selectedViewController = selectedViewController else { return }
guard let navigationController = selectedViewController as? UINavigationController else { return }
guard let viewControllers = navigationController.viewControllers else { return }
let targetViewController = viewControllers.first(where: { $0 is YourTargetViewController })
if let targetViewController = targetViewController {
navigationController.popToViewController(targetViewController, animated: true)
}
}
}
这个代码示例中,需要将 YourTargetViewController
替换为实际需要的 UIViewController 类名。
这个方法可以防止双击 UITabBarController 时自动回到根视图控制器,并且可以在需要的时候回到指定的视图控制器。
领取专属 10元无门槛券
手把手带您无忧上云