【发布时间】:2018-04-10 07:08:40
【问题描述】:
您好(首先对不起我的英语),我在UITableView 上方有一个UIView,当我开始滚动UITableView 时UIView 被隐藏。
我面临的问题是UITableView 将UIView 推到顶部,同时滚动。我想在UIView 被隐藏时开始滚动。
如果您在 gif 中看到第 0 行下方,则第 1 行和第 2 行隐藏在 UIView 之前。
我在寻找什么,如果您看到句子 Swipe Right from left ... 保持在同一高度,直到
UIView被隐藏。 (此配置器来自 Flexbile Header 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
}
}
【问题讨论】:
标签: ios objective-c iphone uitableview uiview