带有CAShapeLayer的UIButton使标题在滚动时消失

时间:2016-07-06 07:19:28

标签: ios swift uibutton cashapelayer

我有ProfileCollectionViewCell一种方法:

func configureCellWithProfile(profile: Profile) {

    shapeLayer.removeFromSuperlayer()

    let path = CGPathCreateMutable()

    CGPathMoveToPoint(path, nil, 0, 0)
    CGPathAddLineToPoint(path, nil, CGRectGetWidth(likeButton.frame), 0)
    CGPathAddLineToPoint(path, nil, CGRectGetWidth(likeButton.frame), CGRectGetHeight(likeButton.frame))
    CGPathCloseSubpath(path)

    shapeLayer.fillColor = profile.favourite ? UIColor.brilliantRose().CGColor : UIColor.blackSqueeze().CGColor
    shapeLayer.path = path

    likeButton.layer.masksToBounds = true
    likeButton.layer.addSublayer(shapeLayer)
}

刚加载后它看起来像这样:

enter image description here

但在我滚动标题后(来自icomoon字体的心脏)消失了:

enter image description here

为什么吗

1 个答案:

答案 0 :(得分:2)

我解决了这个问题,取而代之的是:

likeButton.layer.addSublayer(shapeLayer)

likeButton.layer.insertSublayer(shapeLayer, below: likeButton.titleLabel?.layer)