前言
使用Xcode16 & iOS 18 运行项目后,UITabbarController切换到时候会有一个微小的缩放动画。但是我的App并没有加过相应功能。发现是iOS 18后 系统自己优化的一个动画。在系统音乐App 也加上了这个效果(如下图示)。
但是在有的App上,这个动画反而使App效果不那么好看。于是可以通过一些方法禁用这个动画
切换动画效果
禁用代码
在UITabBarController基类里执行这段代码即可
网上有多种解决方案,目前我使用的这一种并且生效了
final class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 18.0, *) {
delegate = self
}
}
}
extension TabBarController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, animationControllerForTransitionFrom fromVC: UIViewController,
to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
}
extension TabBarController: UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: (any UIViewControllerContextTransitioning)?) -> TimeInterval {
return .zero
}
func animateTransition(using transitionContext: any UIViewControllerContextTransitioning) {
guard let view = transitionContext.view(forKey: .to)
else {
return
}
let container = transitionContext.containerView
container.addSubview(view)
transitionContext.completeTransition(true)
}
}
注意事项
其他页面如果出现
tabBarController?.delegate = self
重设了delegate会导致上面基类的代理不走了一定要注意,可以加点日志看是否执行了代理
参考资料
除了这个方法还在开发者论坛看到了其他方法,但是并未验证过,大家也可以尝试下。
开发者论坛:https://forums.developer.apple.com/forums/thread/763122
其他解决方案:https://medium.com/@adityaramadhan.biz/new-tabbar-transition-animation-in-ios-18-and-xcode-16-ea4b2c4d84d4