提问人:raj 提问时间:4/27/2020 最后编辑:halferraj 更新时间:11/2/2021 访问量:3147
如何在Objective -C iOS中的AVPlayerViewController上添加自定义按钮?
How to add custom button on AVPlayerViewController in Objective -C iOS?
问:
我想在AVPlayerViewController中添加一个按钮,但我无法这样做。我添加了一个按钮,但它不可点击,这是屏幕截图[AVPlayerViewController.contentOverlayView addSubview:_btnHandfree];
也许在上层找不到。
答:
0赞
Athul Sethu
4/27/2020
#1
试试这个。它可能会有所帮助。
#import "ViewController.h"
#import <AVKit/AVKit.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSURL *url=[[NSBundle mainBundle]URLForResource:@"videoplayback" withExtension:@"mov"];
AVPlayer *player = [AVPlayer playerWithURL:url];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake(20, 100, 100, 50)];
[button addTarget:self
action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Button" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor whiteColor]];
[self presentViewController:playerViewController animated:YES completion:^{
[playerViewController.player play];
}];
[self.player addSubview:playerViewController.view];
[self.view addSubview:button];
for(UIWindow* tempWindow in [[UIApplication sharedApplication]windows]){
for(UIView* tempView in [tempWindow subviews]){
if ([[tempView description] rangeOfString:@"UIInputSetContainerView"].location != NSNotFound){
[tempView addSubview:button];
break;
}
}
}
}
-(void)aMethod:(UIButton*)sender {
NSLog(@"test");
}
@end
评论
0赞
raj
4/28/2020
[playerViewController.contentOverlayView addSubview:_btnHandfree]; ..我添加了,但按钮没有响应
0赞
raj
4/28/2020
可能是我找不到的某个层
0赞
Athul Sethu
4/28/2020
检查此编辑。添加按钮后添加 for 循环。
0赞
Athul Sethu
4/28/2020
实际上,您无法将按钮添加到 playerviewController 内容覆盖层。它将显示按钮,但内容覆盖上方还有另一个视图,会阻止您触摸按钮。
0赞
hariszaman
4/28/2020
#2
如 Apple 文档中所述。
在视频内容和播放控件之间显示的视图。
您可以调试视图层次结构以查看按钮在堆栈中的位置。
那么,如果直接将其添加到视图中呢?
[AVPlayerViewController.view addSubview:yourButton];
评论
0赞
oğuz
3/1/2022
不幸的是,它落后于玩家,而不是高于玩家。
0赞
nodebase
11/2/2021
#3
如果查看视图控制器视图的子视图,您将看到它是一个名为 的视图。在制作视图控制器时,可以执行如下操作:AVPlayerViewControllerContentView
func showFullScreenVideo(using player: AVPlayer) {
// setup your player view controller
let playerViewController = AVPlayerViewController()
playerViewController.player = player
present(playerViewController, animated: true) { [weak self] in
self?.addShareButton(to: playerViewController)
}
}
private func addShareButton(to playerViewController: AVPlayerViewController) {
if let playerContentView = playerViewController.view.subviews.first {
let someView = SomeView()
playerContentView.addSubview(someView)
// use your own layout code:
someView.constrain(.centerX, .centerY, to: playerContentView)
}
}
在完成块内添加共享按钮可确保已加载并可供使用。您可以尝试以多种方式强制加载视图。选择权在您手中。另一种可能的方法是在添加共享按钮之前添加以下代码行:playerViewController.view
_ = playerViewController.view
而不是使用完成块。可能有效,也可能无效,但完成块方法绝对有效。
评论