0
你好(我的英文第一个对不起),我有一个UITableView
上方UIView
,而UIView
是隐藏的,当我开始在UITableView
滚动。添加一个UIView以上的tableView并开始滚动时的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
}
}