简体   繁体   中英

UILabel not visible in circular custom UIView

My view hierarchy looks like the following, I have a UITableViewController -> Static Cells -> UITableViewCell -> Custom UIView -> UILabel

My goal is to show circular profile image views and the last view shows a count with the number of remaining images.

That's how I create a circular view which works perfectly fine

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
}

So now I want to create such a view with a UILabel inside

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
}

The outcome looks like this, with no UILabel in the brown view.

UILabel在棕色的UIView中不可见

Frame of circularView : <UIView: 0x7feb28fd9d50; frame = (295 24.75; 30 30); clipsToBounds = YES; ... <UIView: 0x7feb28fd9d50; frame = (295 24.75; 30 30); clipsToBounds = YES; ...

Frame of label : <UILabel: 0x7feb28ce33b0; frame = (295 24.75; 30 30); userInteractionEnabled = NO; ... <UILabel: 0x7feb28ce33b0; frame = (295 24.75; 30 30); userInteractionEnabled = NO; ...

The weird thing is, if I put this code into a playground, it works like expected and the label is visible.

Just for completeness that's how I call these two functions

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)

since your goal is to show profile pictures i would have tried to replace the brown background with the picture:

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

sorry i couldn't help with the label thing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM