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

iOS Swift 如何通过代码实现present视图控制器时全屏展示

解决方案

首先明确问题根源:iOS 13及以上系统默认UIViewController的弹出样式modalPresentationStyle为pageSheet(非全屏卡片样式),你之前修改当前页面属性无效,是因为该属性需要设置给被弹出的目标视图控制器,而非当前发起present的页面。


方案1:代码中手动配置(兼容性最好,优先级最高)

在每个present语句前,给目标VC设置modalPresentationStyle为.fullScreen即可,示例修改如下:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    let settingsSection = SettingsSection(rawValue: indexPath.section)
    switch settingsSection {
    case .social:
        let value = SocialOptions(rawValue: indexPath.row)
        switch value {
        case .editProfile:
            editProfileVC.modalPresentationStyle = .fullScreen
            present(editProfileVC, animated: true)
        case .changePassword:
            changePasswordVC.modalPresentationStyle = .fullScreen
            present(changePasswordVC, animated: true)
        case .deleteAccount:
            deleteVC.modalPresentationStyle = .fullScreen
            present(deleteVC, animated: true)
        default:
            debugPrint("Invalid Selection")
        }
    case .communications:
        let value = CommunicationOptions(rawValue: indexPath.row)
        switch value {
        case .notifications:
            notificationVC.modalPresentationStyle = .fullScreen
            present(notificationVC, animated: true)
        case .language:
            languageVC.modalPresentationStyle = .fullScreen
            present(languageVC, animated: true)
        case .contactUs:
            contactVC.modalPresentationStyle = .fullScreen
            present(contactVC, animated: true)
        default:
            debugPrint("Invalid selection")
        }
    default:
        debugPrint("Invalid selection")
    }
}

优化写法(避免重复代码)

可以抽一个通用的全屏弹出方法,简化调用:

private func presentFullScreen(_ vc: UIViewController, animated: Bool = true, completion: (() -> Void)? = nil) {
    vc.modalPresentationStyle = .fullScreen
    present(vc, animated: animated, completion: completion)
}

之后每个case里直接调用即可:

case .editProfile:
    presentFullScreen(editProfileVC)

方案2:Storyboard配置生效说明

你之前配置未生效大概率是配置对象错误,需要按以下规则确认:

  • 如果目标VC是直接在Storyboard中创建的:选中要弹出的目标VC(不是当前的列表页VC),在Attributes inspector中找到Presentation选项,设置为Full Screen
  • 如果目标VC外层嵌套了UINavigationController:需要给导航控制器设置Presentation为Full Screen,给内部子VC设置无效

常见踩坑排查

如果上述配置都做完还是不生效,检查你要弹出的对象是否为UINavigationController包装后的实例:

// 错误示例:给子VC设置属性,实际弹出的是导航控制器,配置不生效
let nav = UINavigationController(rootViewController: editProfileVC)
editProfileVC.modalPresentationStyle = .fullScreen
present(nav, animated: true)

// 正确示例:给被弹出的导航控制器设置属性
let nav = UINavigationController(rootViewController: editProfileVC)
nav.modalPresentationStyle = .fullScreen
present(nav, animated: true)

内容的提问来源于stack exchange,提问作者Charles Morgan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 13:54:00