I saw a cool feature on YouTube for playing instream ads directly in the system pip
I wanted to repeat their functionality. I was able to add a regular UILabel, but I had problems with the UIButton. The event of clicking on the "Skip" button is not processed
Please tell me how to add the button correctly and handle the event of clicking on it
The YouTube feature is shown in the screenshot
private lazy var button: UIButton = {
let button = UIButton()
button.setTitle("Skip", for: .normal)
button.backgroundColor = .gray
button.tintColor = .white
button.translatesAutoresizingMaskIntoConstraints = false
button.isUserInteractionEnabled = true
button.addTarget(self, action: #selector(handleTap), for: .touchUpInside)
return button
}()
@objc
private func handleTap() {
print("TEST")
}
func pictureInPictureControllerWillStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
if let window = UIApplication.shared.windows.first {
window.addSubview(button)
window.bringSubviewToFront(button)
NSLayoutConstraint.activate([
button.trailingAnchor.constraint(equalTo: window.trailingAnchor, constant: -16),
button.bottomAnchor.constraint(equalTo: window.bottomAnchor, constant: -16),
])
}
}