Custom Button in AVPlayerViewController.ContentOverlayView

I need to add a custom button to the AVPlayerViewController, which will be displayed in both full screen and not full screen for an application with iOS 8.

Adding a button to AVPlayerViewController.view or containing a view will work for non-full-screen mode, but when the player switches to full-screen mode, the button will no longer be visible. I found that if I add a button to AVPlayerViewController.ContentOverlayView, it will appear in full-screen and not full-screen modes, but then it will not be visible that ContentOverlayView is responding to any clicks, so the button cannot be pressed. Does anyone know of another place to add a button or a way to make ContentOverlayView respond to touches?

Code example

AVPlayerViewController *playerView = [[AVPlayerViewController alloc] init];
playerView.player = [AVPlayer playerWithURL:movieURL];
CGRect viewInsetRect = CGRectInset ([self.view bounds],
                                    kMovieViewOffsetX,
                                    kMovieViewOffsetY );
/* Inset the movie frame in the parent view frame. */
[[playerView view] setFrame:viewInsetRect];
[self.view addSubview: [playerView view]];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor yellowColor];
btn.frame = CGRectMake(50, 50, 200, 75);
[btn addTarget:self action:@selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[btn setUserInteractionEnabled:YES];
[btn setEnabled:YES];

[playerView.contentOverlayView addSubview:btn];
+2
source share
2 answers

I managed to get this working by adding the following to my AVPlayerViewController in IB:

  • Overlay view with a button inside it
  • Add constraints to center the button in blend mode.
  • Created an IBOutlet for presentation (I called it "overlay")
  • Created an action from a button on the AVPlayerViewController that prints something so that we can see that clicking the button works.
  • Drag the overlay view from the view hierarchy and onto the top panel of the view controller (displayed as a view icon in the top panel of the view controller, as well as an exit icon, etc.)

and then in the code for AVPlayerViewController:

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.addSubview(self.overlay)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        self.overlay.frame = self.view.bounds
    }

    @IBAction func playBtnTapped(sender: AnyObject) {
        println("PlayBtnTapped")
    }

/ ,

+1

.

:

[playerView.contentOverlayView addSubview:btn];

( playerView)

[self.view addSubview:btn];

.

, , :

self.windowChecker = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(checkIfFullscreen) userInfo:nil repeats:YES];

:

-(void)checkIfFullscreen{
    NSLog(@"videoBounds\n %f %f ", self.avVideoPlayer.contentOverlayView.frame.size.width,self.avVideoPlayer.contentOverlayView.frame.size.height);

    if ([self playerIsFullscreen]) { // check the frame on this
        for(UIWindow* tempWindow in [[UIApplication sharedApplication]windows]){
            for(UIView* tempView in [tempWindow subviews]){

                if ([[tempView description] rangeOfString:@"UIInputSetContainerView"].location != NSNotFound){
                    UIButton *testB = [[UIButton alloc] initWithFrame: CGRectMake(20, 20, 400, 400)];
                    testB.backgroundColor = [UIColor redColor];
                    [testB addTarget:self action:@selector(buttonTouch) forControlEvents:UIControlEventTouchDown];
                    [tempView addSubview:testB];

                    break;
                }
            }
        }
    }
}

, , , , .

!

0

Source: https://habr.com/ru/post/1622209/


All Articles