Регулировка высоты заголовка UICollectionView? - PullRequest
0 голосов
/ 25 октября 2018

Я пытаюсь настроить высоту моего заголовка CollectionView на основе высоты myView .например, я хочу иметь возможность набирать:

    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 :

  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 голосов
/ 25 октября 2018

Каким-то образом вы используете магическое число для высоты myview внутри класса MyProfileHeader, например 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
        }
- - - - - -
}

Теперь вы можете использовать это при возврате макета:

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

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

    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...