2016-07-12 7 views
0

UICollectionViewの各セルの中央にUILableを入れようとしています。ここでUILabelの中心を置きます

は私のコードです:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell 

    // Configure the cell 
    cell.backgroundColor = UIColor.brownColor() 

    let title = UILabel(frame: CGRectMake(cell.bounds.midX, cell.bounds.midY, cell.bounds.size.width, 20)) // 
    title.text = "ddd" 
    title.textColor = UIColor.whiteColor() 

//  title.center = cell.center 

    cell.contentView.addSubview(title) 

//  let xCenterConstraint = NSLayoutConstraint(item: title, attribute: .CenterX, relatedBy: .Equal, toItem: cell, attribute: .CenterX, multiplier: 1, constant: 0) 
//  let yCenterConstraint = NSLayoutConstraint(item: title, attribute: .CenterY, relatedBy: .Equal, toItem: cell, attribute: .CenterY, multiplier: 1, constant: 0) 
//  let leadingConstraint1 = NSLayoutConstraint(item: title, attribute: .Leading, relatedBy: .Equal, toItem: cell, attribute: .Leading, multiplier: 1, constant: 20) 
//  let trailingConstraint1 = NSLayoutConstraint(item: title, attribute: .Trailing, relatedBy: .Equal, toItem: cell, attribute: .Trailing, multiplier: 1, constant: -20) 
//  cell.addConstraints([xCenterConstraint, yCenterConstraint, leadingConstraint1, trailingConstraint1]) 
//  title.centerXAnchor.constraintEqualToAnchor(cell.centerXAnchor).active = true 
//  title.centerYAnchor.constraintEqualToAnchor(cell.centerYAnchor).active = true 


    return cell 
} 

出力:

enter image description here

は、私は自分のコードを変更する必要があるようですので、私は何をしないのですか?

答えて

1

コードに続くことを試みることができるあなたは

let title = UILabel(frame: CGRectMake(0, 0, cell.bounds.size.width, 20)) 
title.textAlignment=.Center 
title.text = "ddd" 
title.textColor = UIColor.whiteColor() 
title.center = cell.contentView.center 
cell.contentView.addSubview(title) 
+0

また、ラベルのテキストをcenterに設定しますか? –

+0

'cell.center = title.center'を' title.center = cell.contentView.center'に変更しました。 –

2
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CollectionViewCell 

    // Configure the cell 
    cell.backgroundColor = UIColor.brownColor() 
//Editing Starts 
    let title = UILabel(frame: CGRectMake(cell.bounds.midX -(cell.bounds.size.width/2) , cell.bounds.midY - 10, cell.bounds.size.width, 20)) 
//Editing Ends 

// 
     title.text = "ddd" 
     title.textColor = UIColor.whiteColor() 

//  title.center = cell.center 

    cell.contentView.addSubview(title) 

//  let xCenterConstraint = NSLayoutConstraint(item: title, attribute: .CenterX, relatedBy: .Equal, toItem: cell, attribute: .CenterX, multiplier: 1, constant: 0) 
//  let yCenterConstraint = NSLayoutConstraint(item: title, attribute: .CenterY, relatedBy: .Equal, toItem: cell, attribute: .CenterY, multiplier: 1, constant: 0) 
//  let leadingConstraint1 = NSLayoutConstraint(item: title, attribute: .Leading, relatedBy: .Equal, toItem: cell, attribute: .Leading, multiplier: 1, constant: 20) 
//  let trailingConstraint1 = NSLayoutConstraint(item: title, attribute: .Trailing, relatedBy: .Equal, toItem: cell, attribute: .Trailing, multiplier: 1, constant: -20) 
//  cell.addConstraints([xCenterConstraint, yCenterConstraint, leadingConstraint1, trailingConstraint1]) 
//  title.centerXAnchor.constraintEqualToAnchor(cell.centerXAnchor).active = true 
//  title.centerYAnchor.constraintEqualToAnchor(cell.centerYAnchor).active = true 


    return cell 
} 
+0

が動作していないのに役立ちます。ラベルは左に移動します –

関連する問題