UILabel在圆形自定义UIView中不可见

时间:2015-07-27 08:48:46

标签: ios swift uitableview uiview uilabel

我的视图层次结构如下所示,我有一个UITableViewController - >静电电池 - > UITableViewCell - >自定义UIView - >的UILabel

我的目标是显示圆形轮廓图像视图,最后一个视图显示剩余图像数量的计数。

这就是我如何创建一个完美无缺的圆形视图

private func getCircularViewForPoint(point: CGPoint) -> UIView {
    var circularView: UIView = UIView(frame: CGRect(x: point.x, y: point.y, width: 30, height: 30))
    circularView.layer.cornerRadius = 15
    circularView.layer.masksToBounds = true

    circularView.backgroundColor = UIColor.blueColor()

    return circularView
}

所以现在我想在里面用UILabel创建这样一个视图

private func getCircularCountViewForPoint(point: CGPoint, maxAmount: Int) -> UIView {
    var circularView = self.getCircularViewForPoint(point)
    circularView.backgroundColor = UIColor.brownColor()

    var label: UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
    label.center = circularView.center
    label.text = "XY"
    label.font = UIFont.systemFontOfSize(10.0)
    label.textAlignment = NSTextAlignment.Center

//        self.addSubview(label)  // This works but label is now behind circularView of course

    circularView.addSubview(label)

    return circularView
}

结果看起来像这样,棕色视图中没有UILabel。

The UILabel is not visible inside the brown UIView

circularView的框架: <UIView: 0x7feb28fd9d50; frame = (295 24.75; 30 30); clipsToBounds = YES; ...

label的框架: <UILabel: 0x7feb28ce33b0; frame = (295 24.75; 30 30); userInteractionEnabled = NO; ...

奇怪的是,如果我将此代码放入游乐场,它会按预期工作,并且标签可见。

为了完整,我称之为这两个函数

for var i = 0; i < maxAmount-1; i++ {
    self.addSubview(self.getCircularViewForPoint(CGPoint(x: xPos, y: yPos)))
    xPos += size+offset
}

// add count view
var countView = self.getCircularCountViewForPoint(CGPoint(x: xPos, y: yPos), maxAmount: maxAmount)
self.addSubview(countView)

1 个答案:

答案 0 :(得分:0)

因为您的目标是显示个人资料图片,我会尝试用图片替换棕色背景:

    circularView.backgroundColor = UIColor(patternImage: UIImage(named: "profilepicturename.png")!)

抱歉,我无法帮助标签事物