Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add cornerRadius to AVPlayerViewController in Swift?

Tags:

i'm working on video player app that written in Swift.

my problem is i should make AVPlayerViewController corners curve. but i want to use just AVPlayerViewController not any other classes.

what i did now:

fileprivate func setupPlayer() {
    let player = AVPlayer(url: videoURL)
    let playerViewController = AVPlayerViewController()
    playerViewController.view.frame = CGRect.init(x: xPosition,
                                                  y: yPosition,
                                                  width: 200,
                                                  height: 100)
    playerViewController.player = player
    self.addChild(playerViewController)
    self.view.addSubview(playerViewController.view)
    playerViewController.didMove(toParent: self)
    playerViewController.videoGravity = AVLayerVideoGravity.init(rawValue: "")
    playerViewController.view.backgroundColor = UIColor(displayP3Red: 0/255, green: 0/255, blue: 0/255, alpha: 0)
    playerViewController.view.layer.cornerRadius = 20
    playerViewController.contentOverlayView?.isHidden = true
    playerViewController.contentOverlayView?.alpha = 0
}

Problem:

what i already have is: enter image description here

Example Solution:

but i want to have something curve like the one in appstore:

enter image description here

like image 547
Mohammad Reza Koohkan Avatar asked Dec 29 '18 08:12

Mohammad Reza Koohkan


1 Answers

The AVPlayerViewController adds a sub layer to your view. When you set the playerViewController.view.layer.cornerRadius property it only affects it and not its children.

enter image description here

To fix it you must clip the subviews (or here the sublayer) by using the masksToBounds layer property.

playerViewController.view.layer.masksToBounds = true

enter image description here

like image 146
Yannick Loriot Avatar answered Nov 15 '22 07:11

Yannick Loriot