在tableView上面添加一个UIView,并在隐藏UIView时开始滚动

时间:2017-10-29 01:23:03

标签: ios objective-c iphone uitableview uiview

您好(首先对不起我的英文),我UIView以上UITableViewUIView开始滚动时隐藏了UITableView

我面临的问题是UITableViewUIView推到顶部并且同时滚动。我想开始滚动隐藏UIView的时间。

如果您在第0行下方的gif中看到第1行和第2行隐藏在UIView之前。

enter image description here

  

我正在寻找的内容,如果您看到句子从左向左滑动... 保持在相同的高度,直到UIView被隐藏。 (此配置程序来自Flexbile标题 Material.io

enter image description here

这是我的代码:

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
}




}

1 个答案:

答案 0 :(得分:3)

您可以尝试此代码

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)

    }
  }
}

点击此链接了解详情https://medium.com/if-let-swift-programming/how-to-create-a-stretchable-tableviewheader-in-ios-ee9ed049aba3