Custom button not working in AVPlayer iOS 11 but it's perfectly working below iOS 11?

元气小坏坏 提交于 2019-12-09 23:45:45

问题


I know possibly this is duplicate question but the question which is already exist didn't have an solution so , I'm asking again to possibly start bounty question .

Here is my code which is working below iOS 11 :

AVPlayer *player = [AVPlayer playerWithURL:videoURL];

// create a player view controller
avcontroller = [[AVPlayerViewController alloc]init];
avcontroller.player = player;
[player play];


NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
[noteCenter addObserverForName:AVPlayerItemDidPlayToEndTimeNotification
                        object:nil
                         queue:nil
                    usingBlock:^(NSNotification *note) {

                        NSLog(@"Video end");
                        self.avcontroller.player = nil;
                        [self.avcontroller.view removeFromSuperview];
                        [self.view addSubview:delegate.tabViewControllerObj.tabBarController.view];
                    }];

_skip =[UIButton buttonWithType:UIButtonTypeCustom];
[_skip addTarget:self action:@selector(btnSkipTapped)
 forControlEvents:UIControlEventTouchUpInside];
[_skip setTitle:@"Skip" forState:UIControlStateNormal];
_skip.layer.borderWidth = 2.0f;
_skip.layer.cornerRadius = 12; 
_skip.clipsToBounds = YES;
_skip.layer.borderColor = [UIColor whiteColor].CGColor;

// show the view controller
[self addChildViewController:avcontroller];
[avcontroller.view addSubview:_skip];
[avcontroller.view bringSubviewToFront:_skip];
[self.view addSubview:avcontroller.view];
avcontroller.view.frame = self.view.frame;

}

-(void)viewDidLayoutSubviews{
  [super viewDidLayoutSubviews];

 _skip.frame = CGRectMake((self.view.frame.size.width-140),(self.view.frame.size.height-150),100.0,35.0);
}

And here is button tapped function:

- (IBAction)btnSkipTapped{

NSLog(@"Skip button clicked");
self.avcontroller.player = nil;
[self.avcontroller.view removeFromSuperview];
[self.view addSubview:delegate.tabViewControllerObj.tabBarController.view];
}

This code will display button perfectly but upon button tap , it didn't call btnSkipTapped function. Any help would be appreciated !


回答1:


@DeepShah you have added skip button in avcontroller.view so its display proper but you can't click on it because AVPlayerViewControllerContentView covers that skip button.

see below image :

so you have to add skip button in self.view and its works.

[self.view addSubview:_skip];
[self.view bringSubviewToFront:_skip];


来源:https://stackoverflow.com/questions/46871529/custom-button-not-working-in-avplayer-ios-11-but-its-perfectly-working-below-io

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!