You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除UINavigationController push与unwind segue的默认动画

解决方案

你可以根据自己的使用场景选择以下任意一种方法实现无动画unwind:

方案1:自定义返回动作(最推荐,适合纯代码场景)

你当前的测试代码用的是系统默认的导航栏返回按钮,默认会带pop动画,直接替换为自定义返回按钮即可控制动画开关:

class DestinationViewController : UIViewController {
    @objc func customBack() {
        navigationController?.popViewController(animated: false)
    }
    
    override func loadView() {
        view = UIView()
        view.backgroundColor = .yellow
        navigationItem.title = "DESTINATION"
        // 替换系统默认返回按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: .plain, target: self, action: #selector(customBack))
    }
}

方案2:适配Storyboard Unwind Segue场景

如果你是通过Storyboard拖拽的unwind segue实现返回,只需要在unwind接收方法中临时关闭全局动画即可:

// 在返回的目标控制器(也就是你的SourceViewController)中添加unwind接收方法
@IBAction func unwindToSource(segue: UIStoryboardSegue) {
    UIView.setAnimationsEnabled(false)
    // 等segue执行完成后恢复动画,避免影响其他页面交互
    DispatchQueue.main.async {
        UIView.setAnimationsEnabled(true)
    }
}

方案3:全局禁用导航控制器转场动画

如果整个导航栈的所有push/pop都不需要动画,直接实现导航控制器代理方法即可:

// 给SourceViewController添加UINavigationControllerDelegate代理
class SourceViewController : UIViewController, UINavigationControllerDelegate {
    @objc func goDestination(_: Any) {
        navigationController?.pushViewController(DestinationViewController(), animated: false)
    }
    
    override func loadView() {
        view = UIView()
        navigationItem.title = "SOURCE"
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(goDestination(_:)))
        // 设置导航控制器代理
        navigationController?.delegate = self
    }
}

内容的提问来源于stack exchange,提问作者Chris Marshall

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 18:39:00