我的UILabel没有显示在模拟器的CALayer中,但显示在视图调试器

时间:2016-11-30 03:48:25

标签: uilabel swift3 calayer xcode8 ios10

我做了一个如下图所示的对话框:

conversation box

我使用此代码创建了一个独特的形状,并将其指定为图层

func bubblePathForContentSize(contentSize: CGSize, left: Bool) -> UIBezierPath {
    let borderWidth : CGFloat = 4 // Should be less or equal to the `radius` property
    let radius : CGFloat = 10
    let triangleHeight : CGFloat = 15
    let rect: CGRect
    let path = UIBezierPath();
    let radius2 = radius - borderWidth / 2 // Radius adjasted for the border width

    if left {
        rect = CGRect(x: 0,y: 0,width: contentSize.width,height: contentSize.height).offsetBy(dx: radius, dy: radius + triangleHeight)
    }else{
        rect = CGRect(x: self.containerView.width
             - contentSize.width - 8 - radius2,y: 0,width: contentSize.width,height: contentSize.height).offsetBy(dx: radius, dy: radius + triangleHeight)
    }

    path.addArc(withCenter: CGPoint(x: rect.maxX,y: rect.minY), radius: radius2, startAngle: CGFloat(-M_PI_2), endAngle: 0, clockwise: true)
    path.addArc(withCenter: CGPoint(x: rect.maxX,y: rect.maxY), radius: radius2, startAngle: 0, endAngle: CGFloat(M_PI_2), clockwise: true)
    path.addArc(withCenter: CGPoint(x: rect.minX,y: rect.maxY), radius: radius2, startAngle: CGFloat(M_PI_2), endAngle: CGFloat(M_PI), clockwise: true)
    path.addArc(withCenter: CGPoint(x: rect.minX,y: rect.minY), radius: radius2, startAngle: CGFloat(M_PI), endAngle: CGFloat(-M_PI_2), clockwise: true)

    if left {
        path.move(to: CGPoint(x: self.containerView.width/3,y: rect.maxY + radius2))
        path.addLine(to: CGPoint(x: self.containerView.width/3 + (triangleHeight/2),y: rect.maxY + radius2 + triangleHeight))
        path.addLine(to: CGPoint(x: self.containerView.width/3 + triangleHeight,y: rect.maxY + radius2))
    }else{
        path.move(to: CGPoint(x: self.containerView.width*2/3,y: rect.maxY + radius2))
        path.addLine(to: CGPoint(x: self.containerView.width*2/3 + (triangleHeight/2),y: rect.maxY + radius2 + triangleHeight))
        path.addLine(to: CGPoint(x: self.containerView.width*2/3 + triangleHeight,y: rect.maxY + radius2))
    }

    let label = UILabel(frame: rect)
    label.textAlignment = .center
    label.text = "Great! Now double tap the image to zoom in."
    label.font = label.font.withSize(11)
    label.textColor = UIColor.white
    self.containerView.addSubview(label)

    path.close()
    return path
}

然后,我想在图层中添加一个UILabel的文本,问题是 UILabel 出现时,当我通过 Xcode 8 中的视图调试器检查时,实际上UILabel出现了,它位于层。 显示调试器中UILabel前面的CALayer

enter image description here 知道如何展示标签吗?我在这里缺少什么属性或方法?

0 个答案:

没有答案