CollectionView作为Swift中的子视图

时间:2017-03-29 07:09:54

标签: ios swift uicollectionview frame subview

CollectionView作为Swift中的子视图

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

enter image description here

这是我的代码:

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)

}

}

1 个答案:

答案 0 :(得分:4)

dataGridView1.RowsDefaultCellStyle.SelectionBackColor = System.Drawing.Color.Black; 

问题是" testView.frame"那。 testview框架x和y指向不是0