現在のコンテキストでモーダルに表示されているコンテナー ビュー内に UITableView が埋め込まれています。ビデオは正常に読み込まれますが、単純にビデオを閉じて UITableView に戻る方法はありません。ビデオを閉じて、元の tableView に戻ることができるようにするオプションが欲しいです。
class MotivationViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, YTPlayerViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(playerView)
playerView.delegate = self
search()
self.tableView.delegate = self
self.tableView.dataSource = self
print(dataArray)
tableView.register(YouTubeTableCell.self, forCellReuseIdentifier: cellID)
view.addSubview(containerView)
view.addSubview(titleLabel)
containerView.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 200, paddingLeft: 20, paddingBottom: 200, paddingRight: 20, width: 0, height: 0)
containerView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
containerView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
containerView.addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.layer.cornerRadius = 20
tableView.anchor(top: containerView.topAnchor, left: containerView.leftAnchor, bottom: containerView.bottomAnchor, right: containerView.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
titleLabel.anchor(top: nil, left: nil, bottom: containerView.topAnchor, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 10, paddingRight: 0, width: 0, height: 0)
titleLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! YouTubeTableCell
let item = titlesArray[indexPath.row]
cell.titleLabel.text = item
let imageURL = URL(string: thumbnailArray[indexPath.row])
cell.thumbnailImageView.sd_setImage(with: imageURL)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = videoID[indexPath.row]
view.addSubview(playerView)
playerView.layer.cornerRadius = 20
playerView.anchor(top: containerView.topAnchor, left: containerView.leftAnchor, bottom: containerView.bottomAnchor, right: containerView.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
let playerVars = ["playsinline": 1] // 0: will play video in fullscreen
self.playerView.load(withVideoId: item, playerVars: playerVars)
}
func dismiss() {
self.playerView.removeFromSuperview()
}
}