简体   繁体   English

标签隐藏在圆圈背景色下

[英]Label is hidden under circle background colour

I have just added a background to my circular progress bar, however this background now covers a label that I have in the centre of the circle. 我刚刚在圆形进度栏中添加了背景,但是该背景现在覆盖了我在圆心中具有的标签。

Basically, I have aa background circle, a radius (for displaying the progress) and a label which is right in the centre of the circle. 基本上,我有一个背景圆圈,一个半径(用于显示进度)和一个标签,该标签正好位于圆圈的中心。 Having now added the circle background means that the label is covered. 现在添加圆圈背景意味着该标签已被覆盖。

Here is my code: 这是我的代码:

class ProgressCircle: UIView {

override func drawRect(rect: CGRect) {
    var ctx = UIGraphicsGetCurrentContext()

    var innerRadiusRatio: CGFloat = 0.6

    var path: CGMutablePathRef = CGPathCreateMutable()
    var startAngle: CGFloat = CGFloat(-M_PI_2)
    var endAngle: CGFloat = CGFloat(-M_PI_2) + min(1.0, progress) * CGFloat(M_PI * 2)
    var outerRadius: CGFloat = CGRectGetWidth(self.bounds) * 0.5 - 1.0
    var innerRadius: CGFloat = outerRadius * innerRadiusRatio
    var center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect))

    //Code for background circle

    var context: CGContextRef = UIGraphicsGetCurrentContext()
    CGContextSetFillColorWithColor(context, (UIColor.darkGrayColor()).CGColor)
    var rectangle: CGRect = CGRectMake(0, 0, 171, 171)
    CGContextBeginPath(context)
    CGContextAddEllipseInRect(context, rectangle)
    CGContextDrawPath(context, kCGPathFill)

    UIGraphicsEndImageContext()

    //Code for progress radius

    CGPathAddArc(path, nil, center.x, center.y, innerRadius, startAngle, endAngle, false)
    CGPathAddArc(path, nil, center.x, center.y, outerRadius, endAngle, startAngle, true)
    CGPathCloseSubpath(path)
    CGContextAddPath(ctx, path)

    CGContextSaveGState(ctx)
    CGContextClip(ctx)
    CGContextDrawImage(ctx, self.bounds, UIImage(named: "RadialProgressFill").CGImage)
    CGContextRestoreGState(ctx)
}

This is how I add the subview: 这就是我添加子视图的方式:

    var circleFrame = CGRect(x: 75, y: 249, width: 171, height: 171)
    var circle = ProgressCircle(frame: circleFrame)
    self.budgetDisplayView.addSubview(circle)

    circle.backgroundColor = UIColor.clearColor()

How can I get the label to be displayed on top of the background? 如何使标签显示在背景顶部?

You need to reorder your subviews. 您需要重新排列子视图。

If you added your ProgressCircle view in Interface Builder, then in the Document Outline in Xcode, drag your label view down below your ProgressCircle view. 如果在Interface Builder中添加了ProgressCircle视图,则在Xcode的文档大纲中,将标签视图向下拖动到ProgressCircle视图下方。 The subviews are added in order, so the later ones are on top. 子视图按顺序添加,因此后面的子视图位于顶部。

If you added your ProgressCircle in code, in your ViewController after adding the subView , call sendSubviewToBack to move the ProgressCircle behind its siblings: 如果添加的ProgressCircle代码,在你ViewController加入后subView ,调用sendSubviewToBack移动ProgressCircle它的兄弟姐妹背后:

self.budgetDisplayView.addSubview(circle)
self.budgetDisplayView.sendSubviewToBack(circle)

Alternatively, if you have an @IBOutlet to your label called myLabel , you can bring it to front: 另外,如果您在标签myLabel有一个@IBOutlet ,则可以将其放在前面:

self.budgetDisplayView.bringSubviewToFront(myLabel)

Finally, make sure your label has a color that will show against the background. 最后,确保标签的颜色会在背景下显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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