【问题标题】:UICollectionView behavior is incorrect as SubviewUICollectionView 行为不正确为子视图
【发布时间】:2016-06-26 09:48:12
【问题描述】:

我在另一个控制器中添加了UICollectionView 作为子视图。我为它设置了框架,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)
    }
}

我的结果是

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

【问题讨论】:

  • 布局、框架高度、项目高度和插入值是多少?
  • @Wain 请在布局代码处掠夺

标签: swift uiview uiviewcontroller uicollectionview


【解决方案1】:

我在 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)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 2021-06-26
    • 1970-01-01
    相关资源
    最近更新 更多