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

Xcode Simulator中跳转新视图后旧视图仍显示的问题排查

问题原因

iOS 13及以上系统中,UIViewController的默认modalPresentationStyle是.pageSheet,这种呈现样式的特性就是:新视图以非全屏形式展示,背景会透出之前的视图,并且支持下拉手势关闭。这正是你遇到的问题根源。

解决方案

要实现全屏覆盖旧视图且禁止下拉关闭的效果,只需要两步:

  • 修改新视图的呈现样式:在自定义Segue的perform方法中,给目标视图控制器设置modalPresentationStyle为.fullScreen,确保它全屏覆盖当前视图。
  • 确保目标视图背景不透明:检查目标视图控制器的view背景颜色,设置为不透明的颜色(比如白色),避免透出下层视图。
修改后的代码

更新你的FadeSegue类代码如下:

import UIKit

class FadeSegue: UIStoryboardSegue {
    override func perform() {
        let source = self.source
        let destination = self.destination
        
        // 设置目标视图全屏呈现
        destination.modalPresentationStyle = .fullScreen
        // 可选:确保目标视图背景不透明(如果你的目标视图背景已经设置过可以忽略)
        destination.view.backgroundColor = .white
        
        let transition = CATransition()
        transition.type = CATransitionType.fade
        transition.duration = 0.7
        
        source.view.window?.layer.add(transition, forKey: kCATransition)
        
        source.present(destination, animated: false, completion: nil)
    }
}
补充说明

如果你的目标视图控制器是导航控制器,直接给导航控制器设置modalPresentationStyle即可,这个样式会自动应用到整个导航栈的呈现上。

内容的提问来源于stack exchange,提问作者Cole Rasmussen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 06:44:56