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

Yas*_* En 2 iphone objective-c uitableview uiview ios

你好(首先为我的英语感到抱歉),我有UIView一个UITableViewUIView当我开始在UITableView.

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

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

在此处输入图片说明

我在找什么,如果你看到句子从左向右滑动......是保持相同的高度,直到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
}




}
Run Code Online (Sandbox Code Playgroud)

tru*_*duc 5

你可以试试这个代码

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)

    }
  }
}
Run Code Online (Sandbox Code Playgroud)

通过此链接了解更多信息https://medium.com/if-let-swift-programming/how-to-create-a-stretchable-tableviewheader-in-ios-ee9ed049aba3