首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加CollectionViewController作为subView

添加CollectionViewController作为subView
EN

Stack Overflow用户
提问于 2020-01-19 00:39:43
回答 2查看 219关注 0票数 0

使用Swift 5.1.3、iOS13.3、XCode11.3、

我尝试显示一个比视图宽度更宽的水平collectionView。通过拖动,collectionView单元格将水平滚动。

现在的问题是:只要我点击拖动,collectionView内容就会“消失”(即变成透明的)。如果你仔细观察,你会发现滚动条还在那里。

为什么我一点击,collectionView内容就会变得透明?

以下是一段视频,说明了错误行为:

下面你会找到相应的代码:

代码如下:

在我的Main-ViewController中,我添加了collectionView:

代码语言:javascript
复制
override func viewDidLoad() {
    super.viewDidLoad()
    let cardsHorizontalController = CardsHorizontalController() 

    self.view.addSubview(cardsHorizontalController.view)

    cardsHorizontalController.view.translatesAutoresizingMaskIntoConstraints = false
    cardsHorizontalController.view.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100.0).isActive = true
    cardsHorizontalController.view.heightAnchor.constraint(equalToConstant: 279).isActive = true
    cardsHorizontalController.view.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width).isActive = true
}

下面是collectionView控制器类:

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

    let cellId = "cellId"

    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.backgroundColor = .clear

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

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

        collectionView.contentInset = UIEdgeInsets(top: 100, left: 0, bottom: 0, right: 0)
    }

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

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return .init(top: 0, left: 16, bottom: 0, right: 0)
    }

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

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

}

及其对应的父类:

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

    init() {
        super.init(collectionViewLayout: UICollectionViewFlowLayout())
    }

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

}

这里是正在发挥作用的collectionView单元:

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

    let imageView = UIImageView(cornerRadius: 8)

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

        imageView.backgroundColor = .red

        let stackView = VerticalStackView(arrangedSubviews: [
            // companyLabel,
            // titleLabel,
            imageView
            ], spacing: 12)
        addSubview(stackView)
        stackView.fillSuperview(padding: .init(top: 16, left: 0, bottom: 0, right: 0))
    }

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

}
EN

回答 2

Stack Overflow用户

发布于 2020-01-19 06:35:04

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

需要将集合视图控制器(即CardsHorizontalController())添加为ChildController

在Main-ViewController的viewDidLoad()中(在上面代码的第一段中),请替换以下行...

代码语言:javascript
复制
self.view.addSubview(cardsHorizontalController.view)

具有以下功能:

代码语言:javascript
复制
self.addChild(cardsHorizontalController)
self.view.addSubview(cardsHorizontalController.view)
self.didMove(toParent: cardsHorizontalController)

这样做之后,一切都像预期的那样工作!

您可以在以下GitHub代码库中找到一个正在运行的项目(仅包含CollectionView):https://github.com/iKK001/HorizontalCollectionViewExample

既然我很高兴--这是一个水平工作CollectionView的视频:

票数 1
EN

Stack Overflow用户

发布于 2020-01-19 01:29:22

您是否尝试过将sizeForItemAt中的return .init(width: view.frame.width - 48, height: view.frame.height)更改为return .init(width: collectionView.frame.width - 48, height: collectionView.frame.height)

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

https://stackoverflow.com/questions/59802636

复制
相关文章

相似问题

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