点击按钮时如何使collectionView标头隐藏?
Posted
技术标签:
【中文标题】点击按钮时如何使collectionView标头隐藏?【英文标题】:How can I make a collectionView header become hidden when a button is tapped? 【发布时间】:2018-07-10 02:20:02 【问题描述】:我试图在点击按钮时隐藏 collectionView 标头,但我似乎无法从 IBAction
函数内部访问此属性。
按钮功能
var buttonPressed:Bool = true
@IBAction func changeView(_ sender: Any)
if buttonPressed
UIView.animate(withDuration: 0.05)
self.buttonPressed = !self.buttonPressed
print("Ive been expanded")
else
UIView.animate(withDuration: 0.05)
self.buttonPressed = !self.buttonPressed
完整的代码
import UIKit
class ViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate
@IBOutlet weak var animateCollectionView: UICollectionView!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
animateCollectionView.dataSource = self
animateCollectionView.delegate = self
animateCollectionView.backgroundColor = UIColor.blue
animateCollectionView.frame = CGRect(x: 0, y: 100, width: 600, height: 1000)
let layout = animateCollectionView.collectionViewLayout as? UICollectionViewFlowLayout
layout?.sectionHeadersPinToVisibleBounds = true
self.view.sendSubview(toBack: self.animateCollectionView)
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
return 200
func numberOfSections(in collectionView: UICollectionView) -> Int
return 1
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
let cell = self.animateCollectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! customCell
cell.cellLabel.text = "\(indexPath.item)"
cell.backgroundColor = UIColor.orange
return cell
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
// returning the search bar for header
let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell", for: indexPath) as! customHeader
header.backgroundColor = UIColor.purple
return header
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize
// if section is above search bar we need to make its height 0
if section == 0
return CGSize(width: 0, height: 0)
// for section header i.e. actual search bar
return CGSize(width: animateCollectionView.frame.width, height: 50)
var buttonPressed:Bool = true
@IBAction func changeView(_ sender: Any)
if buttonPressed
UIView.animate(withDuration: 0.05)
self.buttonPressed = !self.buttonPressed
print("Ive been expanded")
else
UIView.animate(withDuration: 0.05)
class customCell :UICollectionViewCell
@IBOutlet weak var cellLabel: UILabel!
class customHeader: UICollectionReusableView
【问题讨论】:
只返回零标头高度 【参考方案1】:正如@SPatel 提到的,将大小设置为零。
然后设置从单元到 VC 的委托方法,以便 VC 知道使布局无效。
例如:
细胞类
protocol HideHeaderViewDelegate
func hideHeaderView(hide: Bool)
class HeaderView: UICollectionReusableView
var delegate: HideHeaderViewDelegate?
@IBOutlet weak var hideButton: UIButton!
override func awakeFromNib()
super.awakeFromNib()
@IBAction func hideButtonAction(_ sender: Any)
self.frame.size = CGSize.zero
guard let delegate = delegate else return
delegate.hideHeaderView(hide: true)
视图控制器
extension ViewController: HideHeaderView
func hideHeaderView(hide: Bool)
if hide == true
print(hide)
// invalidate your layout here
别忘了设置委托
func collectionView(_ collectionView: UICollectionView,
viewForSupplementaryElementOfKind kind: String,
at indexPath: IndexPath) -> UICollectionReusableView
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader,
withReuseIdentifier: "headerView", for: indexPath) as! HeaderView
headerView.delegate = self
return headerView
【讨论】:
非常感谢您的回复,非常有帮助。我很好奇我是否想通过 viewController 中发生的事件触发标题隐藏让我们说通过 viewController 中的按钮或 collectionView 滚动这可能吗?您将如何设置委托? @TheRedCamaro3.03.0 UICollectionView 是 ScrollView 的子类。你需要这个委托:UIScrollViewDelegate,然后你可以使用它的方法。有关这方面的更多信息,我会搜索 SOF 或发布一个单独的问题。 :)以上是关于点击按钮时如何使collectionView标头隐藏?的主要内容,如果未能解决你的问题,请参考以下文章
CollectionViewCell 顶部的按钮使 collectionView 停止滚动
点击 CollectionView Cell 按钮以播放来自特定阵列的声音并从特定阵列复制电影