以全屏视频为主要内容的ios滚动视图



这是我想要实现的布局

我正试图弄清楚如何在不同的屏幕上使用这种布局。我试着得到屏幕高度,并用程序设置视频层高度等于它。音量按钮的限制是从右边距25,从视频底部25。问题是,当我尝试运行应用程序时,音量按钮显示在屏幕的右中角,而不是右下角。此外,图像视图与视频重叠,而不是显示在视频下面。我的假设是,这些元素的约束是指故事板中视频层的高度,而不是我在ViewController.swift.中编程设置的高度

您可以通过表格视图实现更连贯的布局,并在另一个部分中为全屏视频和其他图像视图提供一个部分。视频部分的单元格高度将为UIScreen.main.bounds.height,这样无论设备如何,您都可以拥有动态全屏高度。

class ViewController: UITableViewController {
var arr: [[String]] = {
var arr = [[String]]()
arr.append(["Video"])
var imageArr = [String]()
for index in 1...20 {
imageArr.append(String(index))
}
arr.append(imageArr)
return arr
}()

override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}

override func numberOfSections(in tableView: UITableView) -> Int {
return arr.count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arr[section].count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = arr[indexPath.section][indexPath.row]
return cell
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0 {
return UIScreen.main.bounds.height
} else {
return 100
}
}
}

至于音量按钮,您可以为视频部分创建一个自定义单元格,并用cellForRowAt方法加载它。确保您使用的是Autolayout而不是CGRect来定位它。

首先,您使用的是ScrollView,因此您需要给屏幕中的每个项目一个高度,这样您就可以给视频屏幕高度"固定高度";然后你也会给你的按钮一个固定的高度,并将其从尾部、前导、底部和顶部连接起来,我认为它会对你起作用,如果出现任何问题,请随时评论

最新更新