ios - 点击按钮时如何隐藏 collectionView header ?

标签 ios swift uicollectionview core-animation uicollectionreusableview

我试图在点击按钮时隐藏 collectionView header ,但我似乎无法从 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 的委托(delegate)方法,以便 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)
    }
}

View Controller

extension ViewController: HideHeaderView {
    func hideHeaderView(hide: Bool) {
        if hide == true {
            print(hide)
            // invalidate your layout here
        }
    }
}

不要忘记设置委托(delegate)

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
    }

关于ios - 点击按钮时如何隐藏 collectionView header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51256396/

相关文章:

ios - Pod 中的重复符号

ios - 如何根据 NSString 日期键在 NSDictionary 下方排序?

ios - 如何从我们的 iOS 应用程序中取消 BLE 设备的配对?

swift - 关闭 UIViewController 后 UI 卡住 5 秒

ios - (Swift) UICollectionViewCell 带有手势识别器的嵌套 subview

ios - 滚动曲线路径

ios - Xcode 4.6 导致 libobold2d-ios.a 在 Kobold2D 中找不到错误

ios - 从附件工具栏中找出当前事件的文本字段

swift - 打印语句删除如何破坏我的项目? ( swift/Xcode)

swift - 使用 Alamofire 通过发布请求传递 token