我已经尝试了所有教程,但是仍然无法以编程方式向按钮添加操作。
我尝试将功能添加到viewdidload和viewdidappear,但是它不起作用。
class videoPlayer: AVPlayerViewController {
// PROPERTIES
let button = UIButton()
.
.
.
override func viewDidLoad() {
super.viewDidLoad()
print("video controller here!")
//Play the Video
let url = Bundle.main.path(forResource: "v1", ofType: "mp4")!
let player = AVPlayer(url: URL(fileURLWithPath: url))
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
playerViewController.showsPlaybackControls = false
}
//ADDING CUSTOM UI
button.setTitle("TEST", for: UIControl.State.normal)
button.backgroundColor = UIColor.red
button.sizeToFit()
button.addTarget(self, action: #selector(self.buttonAction(sender: )), for: .touchUpInside)
playerViewController.contentOverlayView?.addSubview(button)
}
// MARK :- Functions
@objc func buttonAction(sender :UIButton) {
print("Button tapped")
}
.
.
.
}
红色按钮确实出现在左上角,但是单击时什么也没发生。
答案 0 :(得分:1)
尝试设置像这样的框架
button.frame = CGRect(x:20,y:20,width:50,height:50)
答案 1 :(得分:0)
我发现了问题。 在复制一些代码时,我没有意识到我正在创建一个新的avplayercontroller。 而是将self.player =玩家设置为有效!
基本上,这些按钮正在显示,但是它们的边界矩形在新创建的avplayercontroller()下。 在avplayercontrolview中创建avplayercontroller对我来说真是愚蠢。
let videoURL = Bundle.main.path(forResource: "v1", ofType: "mp4")!
let player = AVPlayer(url: URL(fileURLWithPath: videoURL))
self.player = player
self.showsPlaybackControls = false
self.player?.play()