swift - UICollectionView 行为与 Subview 不同

标签 swift uiview uiviewcontroller uicollectionview

我在另一个 Controller 中添加了 UICollectionView 作为 subview 。我为它设置了框架,UICollectionView 流是水平的。我只需要显示 1 行项目并水平滚动。但我垂直得到几行。我尝试了几种不同的方法,但对我没有帮助。我搜索了相关信息,但这些方法也对我没有帮助。我该如何修复它?

我的代码

override func viewDidLoad() {
    super.viewDidLoad()
    addUIElements()
}

// MARK: - UI functions

private func addUIElements() {
    view.addSubview(headerView)

    let lifelineController = StoryboardManager.lifeLineStoryboard.instantiateViewControllerWithIdentifier("LifeLineCollectionViewController") as! LifeLineCollectionViewController
    addChildViewController(lifelineController)
    lifelineController.view.frame = CGRect(x: 0, y: headerView.frame.height, width: headerView.frame.width, height: 100)
    view.addSubview(lifelineController.view)
    lifelineController.didMoveToParentViewController(self)
}

布局代码

    extension LifeLineCollectionViewController: UICollectionViewDelegateFlowLayout {

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        let value = 50
        return CGSize(width: value, height: value)
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
        return 8
    }

//    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
//        return CGSize(width: collectionView.frame.width, height: 376)
//    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        let top: CGFloat = 48
        let cornerValue: CGFloat = 8
        let bottom: CGFloat = 40
        return UIEdgeInsets(top: top, left: cornerValue, bottom: bottom, right: cornerValue)
    }
}

我的结果是 enter image description here

我在 UICollectionView 的 viewDidAppear 中打印了它的框架,得到了完全不同的结果 508 高度,但我将高度设置为 136。

最佳答案

我在 SnapKit 的帮助下解决了这个问题

    private func addUIElements() {
    view.addSubview(headerView)

    let lifelineController = StoryboardManager.lifeLineStoryboard.instantiateViewControllerWithIdentifier("LifeLineCollectionViewController") as! LifeLineCollectionViewController
    addChildViewController(lifelineController)
    view.addSubview(lifelineController.view)
    lifelineController.view.snp_makeConstraints { (make) in
        make.width.equalTo(headerView.frame.width)
        make.height.equalTo(136)
        make.centerX.equalTo(view.snp_centerX)
        make.top.equalTo(headerView.snp_bottom)
    }
    lifelineController.didMoveToParentViewController(self)
}

关于swift - UICollectionView 行为与 Subview 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38037201/

相关文章:

javascript - 如何在 typescript 中获取 Date.timeIntervalSinceReferenceDate?

ios - 我是否应该将 "addArrangedSubview"附在动画 block 中?

swift - 设置 `constraint.isActive = false` 是否解除约束?

iphone - 如何重新加载 UIViewController

Swift SKAction.waitForDuration 执行得太快

ios - React Native iOS 应用程序在重启时重新启动

ios - 如何在 Swift 中压缩 PNG 文件?

uiview - 从 SKView 回到 UIView

ios - 为什么我以编程方式添加的 UIView 没有连接到我的 UIViewController?

ios - 单个 UIViewcontroller 中的两个 UITableView 无法正常工作