2017-02-23 5 views
0

私はViewControllerをを持っており、その中UICollectionViewは、私はプログラム的UICollectionViewにUILableを追加しようとしています:UICollectionView内のUILabelフレームを変更できませんか?

let Label: UILabel = { 
    let label = UILabel() 
    label.text = "Sample Text Here" 
    label.textAlignment = .center 
    label.textColor = UIColor(white: 0.4, alpha: 0.4) 
    label.font = UIFont.systemFont(ofSize: 20) 
    return label 
}() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    Label.translatesAutoresizingMaskIntoConstraints = false 
    Label.frame = CGRect(x: 0, y: 40, width: view.frame.width, height: 20) 
    collectionView.addSubview(Label) 
} 

where the label's position is not the same as x:0 , y: 40

私はそれが同じ

だyの値を変更しようとしたとき、ときに私次のように追加しようとしました:view.addSubView(Label)がまったく表示されませんでした。

注:それは働く細胞

+2

collectionViewにサブビューを追加しないでください。 UICollectionViewCellを作成し、セルに – Bhanupriya

+0

@Bhanupriyaというラベルを追加するセルがない場合はラベルが表示されます –

答えて

1

を移入するデータがありません場合は、ラベルが表示されます。..

override func viewDidLoad() { 
    super.viewDidLoad() 

    let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 300, height: 300), collectionViewLayout: UICollectionViewFlowLayout()) 

    self.view.addSubview(collectionView) 
    collectionView.backgroundColor = UIColor.red 

    let label = UILabel() 
    label.text = "Sample Text Here" 
    label.textAlignment = .center 
    label.textColor = UIColor(white: 0.4, alpha: 0.4) 
    label.font = UIFont.systemFont(ofSize: 20) 

    label.frame = CGRect(x: 0, y: 100, width: view.frame.width, height: 20) 
    collectionView.addSubview(label) 

} 

はいえ、collectionviewに直接ラベルを追加することに注意してください。 あなたはuiviewを作成して、それにラベルとコレクションの両方を追加することができます(もちろん、適切なコレクションビューレイアウトで)。コレクションビューを使用してセルを表示し、セルに何かを追加します。必要な場合はコンテンツビュー

ありがとうございました。

関連する問題