调整 UICollectionView Header 高度?

Adjusting UICollectionView Header height?

我正在尝试根据 myView 的高度调整 CollectionView Header 的高度。例如我希望能够输入:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width: view.frame.width, height: 100)
} //Instead of 100 I want to say: **myView.frame.height**

但是由于我在 header class 中创建了 myView 我无法访问它,我想知道是否有人可以帮助我解决这个问题或者有更好的方法来做到这一点。非常感谢!

这是我的代码:

class MyProfileCollectionView: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView?.register(MyProfileHeader.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerIdentifier)
    }



    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind:
        String, at indexPath: IndexPath) -> UICollectionReusableView {

        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier:
            headerIdentifier, for: indexPath) as! MyProfileHeader
        header.backgroundColor = .red
        return header

    } //HERE
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: view.frame.width, height: 111)
    }

}

这是我的 Header class 我在其中创建 myView:

  class MyProfileHeader: UICollectionReusableView, UICollectionViewDelegate {

        override init(frame: CGRect)    {
            super.init(frame: frame)
            self.addSubview(myView)

            myView.widthAnchor.constraint(equalToConstant: 50).isActive = true
            myView.heightAnchor.constraint(equalToConstant: 70).isActive = true
            myView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
            myView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
        }

//I want my headers height be equal to myView's height so if i change myView's height the headers height changes automatically.
        var myView: UIView = {
            let view = UIView()
            view.translatesAutoresizingMaskIntoConstraints = false
            view.backgroundColor = .gray
            return view
        }()

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }

您在 MyProfileHeader 中使用幻数作为 myview 的高度 class,就像 myView.heightAnchor 是 70。为了快速解决问题,您可以在 MyProfileHeader 内部或外部使用静态变量。

    class MyProfileHeader: UICollectionReusableView, UICollectionViewDelegate {

        static var height: CGFloat = 70.0

        override init(frame: CGRect)    {
            super.init(frame: frame)
            self.addSubview(myView)

            myView.widthAnchor.constraint(equalToConstant: 50).isActive = true
            myView.heightAnchor.constraint(equalToConstant: MyProfileHeader.height).isActive = true
            myView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
            myView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
        }
- - - - - -
}

现在您可以在布局中使用它了 return:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {

        return CGSize(width: view.frame.width, height: MyProfileHeader.height)

    }