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

如何为AVPlayerViewController设置userInteractionEnabled为NO?遇报错求解

解决AVPlayerViewController设置userInteractionEnabled报错的问题

嘿,这个问题我熟!你遇到的报错是因为**AVPlayerViewController类本身并没有setUserInteractionEnabled:这个方法**——这个属性是UIView类的成员,你需要针对控制器的view对象来设置,而不是直接给控制器本身发这个消息。

修正后的代码

把原来错误的那行代码替换成操作控制器view的版本就行:

AVPlayer *player = [AVPlayer playerWithURL:videoURL]; 
AVPlayerViewController *playerViewController = [AVPlayerViewController new]; 
playerViewController.player = player; 

// 正确的写法:设置view的userInteractionEnabled属性
playerViewController.view.userInteractionEnabled = NO;

// 如果你还想隐藏播放控制栏,也可以加上这行(可选)
playerViewController.showsPlaybackControls = NO;

额外提示

如果你的需求只是想禁用播放控制相关的交互(比如不让用户暂停、快进),其实更推荐直接设置showsPlaybackControls = NO,这是AVPlayerViewController专门提供的属性,更符合苹果的设计规范。只有当你需要完全禁用整个控制器视图的所有交互(包括点击、滑动等任何触摸操作)时,才需要设置view.userInteractionEnabled = NO。

内容的提问来源于stack exchange,提问作者ΩlostA

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:03:29