我想在AVPlayerViewController中添加一个按钮,但是我无法做到这一点。我添加了一个按钮[AVPlayerViewController.contentOverlayView addSubview:_btnHandfree];
,但它不可点击,这是屏幕截图
也许在其中找不到上层。
尝试这个。可能会有帮助。
#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
如Apple文档中所述。
显示在视频内容和播放控件之间的视图。
您可以调试视图层次结构以查看按钮在堆栈中的位置。
那么,如果直接将其添加到视图中呢?
[AVPlayerViewController.view addSubview:yourButton];