ios - 当点击按钮时,如何使 collectionView 标头隐藏?
问题描述
我试图在点击按钮时隐藏 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{
}
解决方案
正如@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
}
推荐阅读
- reactjs - 来自父组件中的 fetch API 的响应被传递给子组件是未定义的
- git - 在哪里可以看到“git fetch”的变化?
- macos - 树脂配置在mac上找不到openssl
- logging - 将 Blazor 中的对象记录到控制台
- flutter - Flutter 应用中的列表不响应触摸事件
- angular - 材质侧导航右侧菜单中组件提供的动态内容
- python - 如何在数据单元中创建具有虚线间距和宽度的线条?
- r - 使用比例数据在 mgcv 中运行二项式 GAM 时出错
- string - 将字符串转换为元组 Haskell
- javascript - 使用 ajax 将数组从 javascript 发布到 symfony 控制器