将CollectionViewController添加为subView

时间:2020-01-18 16:39:42

标签: swift scrollview uicollectionviewcell collectionview childviewcontroller

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

我尝试显示水平的collectionView,其宽度大于视图的宽度。 通过拖动,collectionView单元格将水平滚动。

现在的问题如下:当我单击拖动时,collectionView内容“消失”(即变为透明)。如果仔细看,您会发现滚动条仍然存在。

为什么单击一次collectionView内容后它就会变成透明的?

以下是说明错误行为的视频:

下面您将找到相应的代码:

enter image description here

代码如下:

在我的Main-ViewController内部,添加collectionView:

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控制器类:

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
    }

}

及其对应的父类:

class BaseListController: UICollectionViewController {

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

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

}

这里是正在播放的collectionView单元格:

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()
    }

}

2 个答案:

答案 0 :(得分:1)

我终于找到了解决方案:

需要将collectionView-Controller(即allow)添加为 ChildController

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

CardsHorizontalController()

具有以下内容:

viewDidLoad()

这样做,一切都会按预期进行!

您可以在以下GitHub存储库中找到一个工作项目(仅使用CollectionView进行精简):https://github.com/iKK001/HorizontalCollectionViewExample

由于我很高兴-这是工作水平CollectionView的视频:

enter image description here

答案 1 :(得分:0)

您是否尝试过将DataStream<String> messageStream = env.addSource(new FlinkKafkaConsumer082<>(parameterTool.getRequired("topic"), new SimpleStringSchema(), parameterTool.getProperties())); 中的FlinkKafkaConsumer更改为FlinkKafkaConsumer082

相关问题