调整UICollectionView标头高度?

时间:2018-10-24 22:23:00

标签: swift uicollectionview uicollectionreusableview

我正在尝试根据 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**

但是由于我已经在标头类中创建了 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)
    }

}

这是我创建了 myView 的Header类:

  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")
        }
    }

1 个答案:

答案 0 :(得分:0)

以某种方式,您正在MyProfileHeader类(例如myView.heightAnchor)中使用mynumber的幻数作为高度。为快速解决方案,可以在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
        }
- - - - - -
}

现在您可以在布局返回中使用它:

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

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

    }