![](/img/trans.png)
[英]Handle scrolling with UIView on top of UIScrollView, ScrollView wont Scroll
[英]UITableView in UIView in UIScrollView. When scrolling tableview and scroll scrollview too
我想在 tableView 滾動和 scrollView 滾動時進行。
我知道有一個解決方案是使用委托,當 tableView 滾動並使 scrollView contentOfSet 更改但這是我不想要的。
UITableView 在 UIView 在 UIScrollView。 滾動表格視圖時也不要滾動滾動視圖
就像這篇文章一樣,我認為是沒有任何設置只初始化視圖,任何人都可以告訴我實現這個嗎?
這是我初始化視圖的方式:
import UIKit
class ViewController: UIViewController {
let backView = UIScrollView()
let tableView = UITableView()
let testView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(backView)
backView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
backView.contentSize = CGSize(width: UIScreen.main.bounds.width, height: 1000)
backView.addSubview(testView)
testView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 500)
testView.addSubview(tableView)
tableView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 500)
tableView.dataSource = self
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
}
如果您想要層次結構中的多個滾動視圖(tableview/scrollview),那么您應該使內部滾動視圖不可滾動。
在您的情況下,tableview 的高度應等於其內容的高度,以便它不再可滾動。 但是父滾動視圖將有助於滾動。 這將防止滾動沖突。
使用 ContentSizesTableView 代替 TableView。
class ContentSizedTableView: UITableView {
override var contentSize: CGSize {
didSet{
self.invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return contentSize
}
override func reloadData() {
super.reloadData()
invalidateIntrinsicContentSize()
layoutIfNeeded()
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.