如何为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
相关产品推荐
相关产品推荐

