UICollectionView с пользовательскими ячейками, которые не прокручиваются или рендерится должным образом - PullRequest
1 голос
/ 05 июня 2019

Пример:

enter image description here

Я выкладываю все в коде (нет storyboard).У меня есть UIViewController с UICollectionView (этот вид коллекции обычно располагается сверху, но я переместил его вниз, чтобы его было легче увидеть).Этот UICollectionView называется topTabView и использует tabCell с.tabCell являются просто сосудами для пользовательского класса кнопок под названием TabButton.Я подозреваю, что здесь есть более чем одна проблема, поскольку прокрутка не только прерывается, но и вкладки перемещаются и выходят из строя.

ViewController:

class MainViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    let tabCellId = "tabCellId"

    var topTabView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let tabHeight = view.frame.height / 45

        //self.view.frame.height / 27 is a hack for the top layout guide
        let topLayoutGuideY = view.frame.height / 27

        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal

        topTabView = UICollectionView(frame: CGRect(x: 0, y: 300, width: self.view.frame.width - tabHeight, height: tabHeight), collectionViewLayout: layout)
        topTabView.bounds = CGRect(x: 0, y: 0, width: topTabView.frame.width, height: topTabView.frame.height)

        topTabView.dataSource = self
        topTabView.delegate = self
        topTabView.isUserInteractionEnabled = true

        topTabView.register(tabCell.self, forCellWithReuseIdentifier: tabCellId)           
        view.addSubview(topTabView)
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 15  //just for testing
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: tabCellId, for: indexPath) as! tabCell

        //cell.button.setTitle(Tabs.SharedInstance.tabsToPersist[indexPath.item].tabName, for: .normal)
        cell.button.setTitle("test" + String(indexPath.item), for: .normal)

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: topTabView.frame.width / 7, height: topTabView.frame.height)
    }
}

tabCell:

class tabCell: UICollectionViewCell {

    var button: TabButton

    override init(frame: CGRect) {
        button = TabButton(frame: frame)
        button.setTitle("Tutorial", for: .normal)

        super.init(frame: frame)

        print(self.frame)
        addSubview(button)
    }
    //Left Out Required Init Coder for Space
}

Кнопка Tab:

class TabButton: UIButton {

    // Inits
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupButton()
    }
    //Removed Required Init Coder for Space
    //Overrides
    override func draw(_ rect: CGRect) {
        //Long Custom Draw Function, confirmed working fine
    }

    func setupButton() {
        backgroundColor = UIColor.clear
        self.setTitleColor(.black, for: .normal)
        self.titleLabel?.font = UIFont.systemFont(ofSize: 9)
    }
}

1 Ответ

0 голосов
/ 05 июня 2019

Пожалуйста, сделайте clipsToBounds true

topTabView.clipsToBounds = true

Пожалуйста, проверьте это видео, все в порядке, или я забыл что-нибудь сделать, пожалуйста, дайте мне знать

enter image description here

...