带有自定义单元格的UICollectionView无法正确滚动或渲染

时间:2019-06-05 18:07:08

标签: ios swift cocoa-touch uicollectionview uicollectionviewcell

示例:

enter image description here

我正在用代码布置所有内容(没有storyboard)。我有一个UIViewController和一个UICollectionView(此collectionview通常位于顶部,但是我已将其向下移动,因此更容易看到)。该UICollectionView被称为topTabView,它使用tabCell s。 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
}

TabButton:

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 :(得分:0)

请将clipsToBounds设为true

topTabView.clipsToBounds = true

请检查该视频是否正常,或者我忘了做任何事情,请告诉我

enter image description here