[英]add a UIView above tableView and start scrolling when UIView is hidden
您好(第一次抱歉,我的英語不好),我在UITableView
上方有一個UIView
,當我開始在UITableView
滾動時, UIView
被隱藏了。
我面臨的問題是, UITableView
將UIView
推到頂部,同時滾動。 我想開始滾動隱藏UIView
的時間。
如果您在gif中看到第0行,第1行和第2行在UIView
之前被隱藏。
我在尋找什么,如果您看到句子從左向右滑動...保持相同的高度,直到
UIView
被隱藏。 (此配置器來自Flexbile標頭Material.io )
這是我的代碼:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
lazy var redView: UIView = {
let view = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 200))
view.backgroundColor = .red
return view
}()
lazy var tableView: UITableView = {
let tableView = UITableView(frame: CGRect(x: 0, y: 200, width: self.view.frame.width, height: self.view.frame.height - 100))
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(redView)
self.view.addSubview(tableView)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.delegate = self
tableView.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
cell.textLabel?.text = "row \(indexPath.row)"
return cell
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offset = scrollView.contentOffset.y
if(offset > 200){
self.redView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 0)
}else{
self.redView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 200 - offset)
}
let rect = CGRect(x: 0, y: self.redView.frame.maxY, width: self.view.bounds.size.width, height:(self.view.bounds.size.height - (self.redView.frame.maxY)))
tableView.frame = rect
}
}
您可以嘗試此代碼
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {
lazy var redView: UIView = {
let view = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 200))
view.backgroundColor = .red
return view
}()
lazy var tableView: UITableView = {
let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(redView)
self.view.addSubview(tableView)
tableView.backgroundColor = UIColor.clear
tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.delegate = self
tableView.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
cell.textLabel?.text = "row \(indexPath.row)"
return cell
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offset = scrollView.contentOffset.y
if(offset > 200){
self.redView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 0)
}else{
self.redView.frame = CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: 200 - offset)
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.