【问题标题】:CollectionView as a Subview in SwiftCollectionView 作为 Swift 中的子视图
【发布时间】:2017-03-29 07:09:53
【问题描述】:

CollectionView 作为 Swift 中的子视图

我尝试插入一个非常简单的 collectionView 作为子视图,但无法获得正确的框架。我究竟做错了什么?你能帮帮我吗?

这是我的代码:

import UIKit

class ViewController: UIViewController {

var collectionView: UICollectionView!

@IBOutlet weak var testView: UIView!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.



}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewDidLayoutSubviews() {

    setupCollectionView()

}

func setupCollectionView() {

    let layout = UICollectionViewFlowLayout()
    collectionView = UICollectionView(frame: testView.frame, collectionViewLayout: layout)
    collectionView.backgroundColor = UIColor.green
    testView.addSubview(collectionView)

}

}

【问题讨论】:

  • 调用 setupCollectionView() 到 viewDidLoad()
  • 试试testView.bounds

标签: ios swift uicollectionview frame subview


【解决方案1】:
func setupCollectionView() {

    let layout = UICollectionViewFlowLayout()
    collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, testView.frame.size.width, height: testView.frame.size.height), collectionViewLayout: layout)
    collectionView.backgroundColor = UIColor.green
    testView.addSubview(collectionView)
    testView.clipsToBounds=true

    }

问题是“testView.frame”那个。 testview 框架 x 和 y 点不是 0

【讨论】:

  • 谢谢!只需稍加修正即可:CGRect(x: 0, y: 0, width: testView.frame.size.width, height: testView.frame.size.height),
  • @Roman 如果它帮助并解决了您的问题,您应该接受并投票赞成答案!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多