我是iOS开发的新手,我正在构建自定义UIFlowLayout,我目前正在构建自定义单元格对象,其中包含图像和标签。
我想在UIImage下绘制标签,但我不知道如何将其直接锚定在UIImage下。
我想要实现的布局如下:
===========================
[ Navigation Bar ]
===========================
[ Image ] [ Image ]
[ Label ] [ Label ]
[ Image ] [ Image ]
[ Label ] [ Label ]
[ Image ] [ Image ]
[ Label ] [ Label ]
===========================
[ Tab Bar ]
===========================
我目前用于UICollectionViewCell
子类的代码如下:
var label : UILabel!
var imageView : UIImageView!
override init(frame: CGRect)
{
// Initialize the frame object
super.init(frame: frame)
self.backgroundColor = UIColor.grayColor()
// Set up the image
imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height))
imageView.contentMode = UIViewContentMode.ScaleAspectFill
imageView.clipsToBounds = true
contentView.addSubview(imageView)
// Make the rectangle a circle by adjusting the corner radius, relative to 50% of the bounds
self.layer.cornerRadius = (frame.size.width / 100) * 50
// Set up the label (how do I do the anchoring?)
label = UILabel(frame: frame)
label.font = UIFont.systemFontOfSize(UIFont.smallSystemFontSize())
label.textAlignment = .Center
label.text = "Alexander Sims"
contentView.addSubview(label)
}
答案 0 :(得分:1)
设置标签的框架时,可以使用imageView.frame作为参考。
这样的事情:
let labelX = imageView.frame.origin.x
let labelY = imageView.frame.origin.y + imageView.frame.height
label = UILabel(frame: CGRectMake(labelX, labelY, imageView.frame.width, 21)) // The last variable, 21, is the label's height. Change as you see fit.