How can I make a view go full screen like the YouTube app?

250 Views Asked by At

I have made a subclass of UIView that automatically adds a AVPlayerLayer along with the video controls. I need to implement a full screen button that takes the UIView and rotates it into fullscreen just like the YouTube app. The problem is the UIView does not cover the whole screen. I have a XLPagerTab below the UIView that contains the overview and stuff for the video.

This is my code to make the UIView full screen. It is inside the VideoPlayer Class which is a subclass of UIView. isExpanded is a boolean to know the state, smallScreenCenter is used to store the initial center of the view to switch back to the small screen.

func fullScreenTapped() {
    
    if !isExpanded {
        //Expand the video
        UIView.animate(withDuration: 0.8, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
            self.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.height, height: UIScreen.main.bounds.width)
            self.center = CGPoint(x: UIScreen.main.bounds.size.width * 0.5, y: UIScreen.main.bounds.size.height * 0.5)
            self.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))
            self.layoutSubviews()
            
        }, completion: nil)
    } else {
        //Shrink the video again
        UIView.animate(withDuration: 0.8, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
            //16 x 9 is the aspect ratio of all HD videos
            self.transform = CGAffineTransform.identity
            self.center = self.smallScreenCenter!
            
            let height = UIScreen.main.bounds.width * 9 / 16
            let videoPlayerFrame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: height)
            self.frame = videoPlayerFrame
            
            self.layoutSubviews()
            
        }, completion: nil)
    }
    
    isExpanded = !isExpanded
    
}

The view rotates and all but does not cover the whole screen, it just looks like this.

After tapping on fullscreen

Is the XLPagerTab interfering with the fullscreen? I am confused.

0

There are 0 best solutions below