通过编程XCODE嵌入YouTube视频
我是Xcode的初学者,我想在Xcode(Storyboard)上以编程方式嵌入YouTube视频。 我在视图控制器上写了这一点,但它不起作用:
import UIKit
import youtube_ios_player_helper
class ViewController: UIViewController {
var playerView: YTPlayerView!
private let myView: YTPlayerView = {
let myView = YTPlayerView()
myView.translatesAutoresizingMaskIntoConstraints = false
myView.backgroundColor = .link
return myView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .cyan
playerView?.load(withVideoId: "bsM1qdGAVbU&")
view.addSubview(myView)
addConstraints()
}
private func addConstraints() {
var constraints = [NSLayoutConstraint]()
constraints.append(myView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 60))
constraints.append(myView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -60))
constraints.append(myView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -500))
constraints.append(myView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 60))
NSLayoutConstraint.activate(constraints)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对您正在使用的
youtube_ios_player_helper
一无所知,所以这只是一个有教养的猜测。但是我认为问题在于您既有playerview
andmyview
ytplayerview s。playerview?.load(withVideoid:“ bsm1qdgavbu& amp;“”)
> playerview 从未初始化myview
,但是切勿调用myview.load(withVideoid:“ bsm1qdgavbu& amp;“”)
我会摆脱
playfiew
,然后将您的ViewDidload更新为callmyview.load(withVideoid)(withVideoid:“ bsm1qdgavbu&amp;“)<”)<)<) /代码>而是。
I know nothing about the
youtube_ios_player_helper
you're using, so this is just an educated guess. But I think the problem is that you have bothplayerView
andmyView
, which are bothYTPlayerView
s.playerView?.load(withVideoId: "bsM1qdGAVbU&")
butplayerView
is never initializedmyView
, but then never callmyView.load(withVideoId: "bsM1qdGAVbU&")
I would get rid of
playerView
and update your viewDidLoad to callmyView.load(withVideoId: "bsM1qdGAVbU&")
instead.