iOS 16中AVPlayerViewController关闭按钮失效问题求助
iOS 16 下 AVPlayerViewController 关闭按钮无响应问题修复
问题原因
iOS 16 调整了 AVPlayerViewController 的交互逻辑,当以全屏模态(UIModalPresentationFullScreen)方式呈现时,系统不再自动处理左上角关闭按钮的 dismiss 操作,需要开发者手动实现对应的关闭逻辑。
解决步骤
- 让当前控制器遵守
AVPlayerViewControllerDelegate协议 - 为 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
相关产品推荐
相关产品推荐

