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

iOS 16中AVPlayerViewController关闭按钮失效问题求助

iOS 16 下 AVPlayerViewController 关闭按钮无响应问题修复

问题原因

iOS 16 调整了 AVPlayerViewController 的交互逻辑,当以全屏模态(UIModalPresentationFullScreen)方式呈现时,系统不再自动处理左上角关闭按钮的 dismiss 操作,需要开发者手动实现对应的关闭逻辑。

解决步骤

  1. 让当前控制器遵守 AVPlayerViewControllerDelegate 协议
  2. 为 AVPlayerViewController 设置代理,并实现关闭按钮点击的代理方法

修改后的代码示例

// 在控制器的.m文件中用类扩展声明遵守协议
@interface YourViewController () <AVPlayerViewControllerDelegate>
@end

@implementation YourViewController

- (void)presentPlayerViewController {
    AVPlayerViewController *vc = [[AVPlayerViewController alloc] init];
    vc.player = [AVPlayer playerWithURL:[NSURL URLWithString:videoInfo.VideoURL]];
    vc.modalPresentationStyle = UIModalPresentationFullScreen;
    vc.delegate = self; // 设置代理
    [self presentViewController:vc animated:NO completion:nil];
}

// 实现关闭按钮点击的代理方法,手动处理关闭逻辑
- (void)playerViewControllerDidTapCloseButton:(AVPlayerViewController *)playerViewController {
    [playerViewController dismissViewControllerAnimated:YES completion:nil];
}

@end

补充说明

如果业务场景允许,也可以将 modalPresentationStyle 设置为 UIModalPresentationPageSheet,此时系统会自动处理关闭按钮的 dismiss 逻辑,但呈现样式会变为半屏弹窗,可根据需求选择。

内容的提问来源于stack exchange,提问作者M. Wilhelm

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 13:40:42