4

On button press I'm presenting an AVPlayerViewController:

func playVideoPressed(sender: UIButton){
  let videoURL = NSURL(string: "someUrl")
  let player = AVPlayer(URL: videoURL!)
  let playerViewController = AVPlayerViewController()
  playerViewController.player = player
  self.presentViewController(playerViewController, animated: true) {
    playerViewController.player!.play()
  }
}

This AVPlayerViewController's status bar makes the UI of the current View Controller jump. Can I hide the statusbar from within this "presentViewController call?

I've tried including

playerViewController.prefersStatusBarHidden()

but the player continues to show the status bar.

Thanks

robinyapockets
  • 363
  • 5
  • 21

3 Answers3

1

And this is version in Objective-C:

@interface MyAVPlayerViewController: AVPlayerViewController
@property (nonatomic) BOOL presenting;
@end

@implementation MyAVPlayerViewController

- (instancetype)init {
    self = [super init];
    if (self) {
        self.presenting = YES;
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.presenting = NO;
    [self setNeedsStatusBarAppearanceUpdate];
}

- (BOOL)prefersStatusBarHidden {
    return !self.presenting && [super prefersStatusBarHidden];
}

@end
kotvaska
  • 639
  • 7
  • 11
0

I ended up simply creating a new AVPlayerViewController that I segue to from button press, and within that Controller I added the code:

override func prefersStatusBarHidden() -> Bool {
    return true
}

Seems like more code, considering I'm just viewing a video, but it works smooth.

robinyapockets
  • 363
  • 5
  • 21
  • are you saying that you subclassed avplayerviewcontroller and added this method or did you add this in the view controller that presented avplayerviewcontroller? – Charlton Provatas Jan 07 '17 at 19:46
0

My solution:

class MyAVPlayerViewController: AVPlayerViewController {
var presenting: Bool = true

override var prefersStatusBarHidden: Bool {
    if presenting {
        return false
    } else {
        return super.prefersStatusBarHidden
    }
}

override var childViewControllerForStatusBarHidden: UIViewController? {
    if presenting {
        return nil
    } else {
        return super.childViewControllerForStatusBarHidden
    }
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    presenting = false
    setNeedsStatusBarAppearanceUpdate()
}
}
Alex
  • 223
  • 2
  • 4