首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CollectionView单元格不可见

CollectionView单元格不可见
EN

Stack Overflow用户
提问于 2020-01-29 21:47:29
回答 1查看 115关注 0票数 0

使用Swift 5.1.3、iOS13.3、

我正在尝试将一个水平控制器显示为ChildView CollectionViewController。

问题:所有的单元格都是不可见的!

代码中的print-语句(显示了许多!在下面的评论中)

代码语言:javascript
复制
Test.CardHeaderCell: 0x7fa563709370; baseClass = UICollectionViewCell; frame = (1059 23; 343 279); hidden = YES; layer = <CALayer: 0x6000024de080>>

代码如下:

代码语言:javascript
复制
override func viewDidLoad() {
    // ...

    let cardsHorizontalController = CardsHorizontalController()
    self.addChild(cardsHorizontalController)
    self.view.addSubview(cardsHorizontalController.view)
    self.didMove(toParent: cardsHorizontalController)
    cardsHorizontalController.view.translatesAutoresizingMaskIntoConstraints = false
    cardsHorizontalController.view.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 70.0).isActive = true
    cardsHorizontalController.view.heightAnchor.constraint(equalToConstant: 390.0).isActive = true
    cardsHorizontalController.view.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width).isActive = true
}

这是HorizontalController

代码语言:javascript
复制
class CardsHorizontalController: HorizontalSnappingController, UICollectionViewDelegateFlowLayout {

    let cellId = "horizontalCardID"

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.backgroundColor = .green

        collectionView.register(CardHeaderCell.self, forCellWithReuseIdentifier: cellId)

        if let layout = collectionViewLayout as? UICollectionViewFlowLayout {
            layout.scrollDirection = .horizontal
        }

        collectionView.showsHorizontalScrollIndicator = true
        collectionView.showsVerticalScrollIndicator = false

        // do the insets here instead of the optional "insetForSectionAt" delegate-method
        // i.e. this helps to get the scrolled cells aligned in the middle of the screen once 1 cell wide scrolled...
        collectionView.contentInset = .init(top: 65.0, left: 16.0, bottom: 0.0, right: 16.0)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return .init(width: view.frame.width - 32.0, height: 279.0)
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 17
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)

        // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        print(cell)
        // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        return cell
    }

}

和Cell:

代码语言:javascript
复制
class CardHeaderCell: UICollectionViewCell {

    let imageView = UIImageView(cornerRadius: 10.0)

    override init(frame: CGRect) {
        super.init(frame: frame)

        // blur view
        imageView.backgroundColor = UIColor(red: 0.086, green: 0.086, blue: 0.086, alpha: 0.35)
        let visualEffectView = UIVisualEffectView(frame: imageView.frame)
        visualEffectView.effect = UIView.customBlurEffect()
        visualEffectView.autoresizingMask = UIView.AutoresizingMask(rawValue: UIView.AutoresizingMask.flexibleWidth.rawValue | UIView.AutoresizingMask.flexibleHeight.rawValue)
        imageView.addSubview(visualEffectView)

        let stackView = VerticalStackView(arrangedSubviews: [
            imageView
        ], spacing: 12.0)
        addSubview(stackView)
        stackView.fillSuperview(padding: .init(top: 0.0, left: 0.0, bottom: 0.0, right: 0.0))
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError()
    }   
}

出于完整性的原因,这里是其余的自定义类...

代码语言:javascript
复制
class HorizontalSnappingController: UICollectionViewController {

    init() {
        let layout = BetterSnappingLayout()
        layout.scrollDirection = .horizontal
        super.init(collectionViewLayout: layout)
        collectionView.decelerationRate = .fast
    }

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

}
代码语言:javascript
复制
class BetterSnappingLayout: UICollectionViewFlowLayout {

    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {

        guard let collectionView = collectionView else {
            return super.targetContentOffset(forProposedContentOffset: proposedContentOffset, withScrollingVelocity: velocity)
        }

        let nextX: CGFloat

        if proposedContentOffset.x <= 0 || collectionView.contentOffset == proposedContentOffset {
            nextX = proposedContentOffset.x
        } else {
            nextX = collectionView.contentOffset.x + (velocity.x > 0 ? collectionView.bounds.size.width : -collectionView.bounds.size.width)
        }

        let targetRect = CGRect(x: nextX, y: 0, width: collectionView.bounds.size.width, height: collectionView.bounds.size.height)

        var offsetAdjustment = CGFloat.greatestFiniteMagnitude

        let horizontalOffset = proposedContentOffset.x + collectionView.contentInset.left

        let layoutAttributesArray = super.layoutAttributesForElements(in: targetRect)

        layoutAttributesArray?.forEach({ (layoutAttributes) in
            let itemOffset = layoutAttributes.frame.origin.x
            if fabsf(Float(itemOffset - horizontalOffset)) < fabsf(Float(offsetAdjustment)) {
                offsetAdjustment = itemOffset - horizontalOffset
            }
        })

        return CGPoint(x: proposedContentOffset.x + offsetAdjustment, y: proposedContentOffset.y)
    }

}
代码语言:javascript
复制
class VerticalStackView: UIStackView {

    init(arrangedSubviews: [UIView], spacing: CGFloat = 0, alignment: UIStackView.Alignment = .center) {
        super.init(frame: .zero)

        arrangedSubviews.forEach({addArrangedSubview($0)})

        self.spacing = spacing
        self.alignment = alignment
        self.axis = .vertical
    }

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

}
EN

回答 1

Stack Overflow用户

发布于 2020-02-03 16:39:02

我终于找到了一个解决方案:

VerticalStackView有一个默认的对齐属性.center

当我将.center替换为.fill作为默认值时,一切都会按预期运行!

VerticalStackView的用法保持不变(即应用对齐的默认值):

代码语言:javascript
复制
let stackView = VerticalStackView(arrangedSubviews: [
    imageView
], spacing: 12.0)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59968462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档