放置UILabel父母的中心

时间:2016-07-12 06:35:13

标签: ios swift uicollectionview

我正在尝试将UILable放在UICollectionView的每个单元格的中心。

这是我的代码:

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

似乎我需要更改我的代码,所以我缺少什么?

2 个答案:

答案 0 :(得分: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
} 

答案 1 :(得分: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)