swift 中视图控制器的对象隐藏在视频背景后面

objects of view controller hide behind of video background in swift

我想为我的应用程序创建一个登录页面,在这种情况下,我想使用视频作为登录页面的背景,例如 vine、spotify、uber... 我播放视频没有问题,并在故事板中添加一些按钮和标签到我的视图控制器,在 运行 应用程序之后我看不到视图控制器的对象 我只是看到视频和隐藏在视频背景后面的对象如何解决这个问题, 感谢帮助

这是我播放视频的代码,

 lazy var playerLayer:AVPlayerLayer = {

    let player = AVPlayer(URL:  NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("BWWalkthrough", ofType: "mp4")!))
    player.muted = true
    player.allowsExternalPlayback = false
    player.appliesMediaSelectionCriteriaAutomatically = false
    var error:NSError?

    // This is needed so it would not cut off users audio (if listening to music etc.
    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
    } catch var error1 as NSError {
        error = error1
    } catch {
        fatalError()
    }
    if error != nil {
        print(error)
    }

    var playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = self.view.frame
    playerLayer.videoGravity = "AVLayerVideoGravityResizeAspectFill"
    playerLayer.backgroundColor = UIColor.blackColor().CGColor
    player.play()
    NSNotificationCenter.defaultCenter().addObserver(self, selector:"playerDidReachEnd", name:AVPlayerItemDidPlayToEndTimeNotification, object:nil)
    return playerLayer
    }()


    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.view.layer.addSublayer(self.playerLayer)


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewWillDisappear(animated: Bool) {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
    playerLayer.frame = self.view.frame
}
func playerDidReachEnd(){
    self.playerLayer.player!.seekToTime(kCMTimeZero)
    self.playerLayer.player!.play()

}

您需要在故事板加载时创建的所有视图下方添加 playerLayer。正在替换

self.view.layer.addSublayer(self.playerLayer)

self.view.layer.insertSublayer(self.playerLayer, atIndex:0)

应该完成这项工作,除非您确实希望在视频层后面有任何视图控制器视图的子视图。