【问题标题】:add a UIView above tableView and start scrolling when UIView is hidden在 tableView 上方添加一个 UIView 并在 UIView 隐藏时开始滚动
【发布时间】:2018-04-10 07:08:40
【问题描述】:

您好(首先对不起我的英语),我在UITableView 上方有一个UIView,当我开始滚动UITableViewUIView 被隐藏。

我面临的问题是UITableViewUIView 推到顶部,同时滚动。我想在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


    【解决方案1】:

    你可以试试这个代码

    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

    【讨论】:

    • 这使得 redview 不可点击:(
    • 你需要先添加tableview然后是redView
    猜你喜欢
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    相关资源
    最近更新 更多