2017-10-29 152 views
0

你好(我的英文第一个对不起),我有一个UITableView上方UIView,而UIView是隐藏的,当我开始在UITableView滚动。添加一个UIView以上的tableView并开始滚动时的UIView隐藏

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

如果你在gif下面看到第0行,第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

你可以试试这个代码

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