簡體   English   中英

在tableView上方添加一個UIView並在隱藏UIView時開始滾動

[英]add a UIView above tableView and start scrolling when UIView is hidden

您好(第一次抱歉,我的英語不好),我在UITableView上方有一個UIView ,當我開始在UITableView滾動時, UIView被隱藏了。

我面臨的問題是, UITableViewUIView推到頂部,同時滾動。 我想開始滾動隱藏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)

    }
  }
}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM