呈现AVPlayerViewController StatusBar使UI跳转

时间:2016-08-23 10:23:09

标签: swift viewcontroller xcode-storyboard avplayerviewcontroller

按下按钮我正在呈现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()
  }
}

此AVPlayerViewController的状态栏使当前视图控制器的UI跳转。我可以在“presentViewController调用中隐藏状态栏吗?

我试过包括

playerViewController.prefersStatusBarHidden()

但玩家继续显示状态栏。

由于

3 个答案:

答案 0 :(得分:0)

我最终只是创建了一个新的AVPlayerViewController,我从按钮按下来,并在该控制器中添加了代码:

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

似乎更多代码,考虑到我只是在观看视频,但它运作顺畅。

答案 1 :(得分:0)

我的解决方案:

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()
}
}

答案 2 :(得分:0)

这是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