1

我在9 天前发布了这个问题。

我还没弄明白,所以我用代码和屏幕截图重新发布。

我有一个自定义按钮,可以在主机应用程序中正常工作,但不能在 Today Extension 中完全绘制。

以下是它在情节提要中的外观以及预览看起来如何失真:

在此处输入图像描述

这是它在 iPhone 5s 上的实际外观:

在此处输入图像描述

我认为这与 Today Extension 的加载方式有关 - 不知何故,按钮没有被完全绘制。然而,这并不能解释失真的预览(至少对我的新手来说)。

这是按钮代码:

@IBDesignable class TakeButton: UIButton {

@IBInspectable var fillColor: UIColor = UIColor.greenColor()
@IBInspectable var isAddButton: Bool = true
@IBInspectable var isTodayExtensionButton: Bool = false

var plusHeight: CGFloat = 0.0
var plusWidth: CGFloat = 0.0

//    
//     Only override drawRect: if you perform custom drawing.
//     An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {

    var path = UIBezierPath(ovalInRect: rect)
    fillColor.setFill()
    path.fill()

    //set up width and height variables for horizontal stroke

    if isTodayExtensionButton {
        plusHeight = 2.0
        plusWidth = 25.0
    }
    else {
        plusHeight = 3.0
        plusWidth = 45.0
    }

    //create the path
    var plusPath = UIBezierPath()

    //set the path's line width to the height of the stroke
    plusPath.lineWidth = plusHeight

    //move the initial point of the path
    //to the start of the horizontal stroke
    plusPath.moveToPoint(CGPoint(
        x: bounds.width/2 - plusWidth/2 + 0.5,
        y: bounds.height/2))

    //add a point to the path at the end of the stroke
    plusPath.addLineToPoint(CGPoint(
        x: bounds.width/2 + plusWidth/2 + 0.5,
        y: bounds.height/2))

    //for vertical line - take button

    if isAddButton {

        plusPath.moveToPoint(CGPoint(
            x: bounds.width/2,
            y: bounds.height/2 - plusWidth/2 + 0.5))

        plusPath.addLineToPoint(CGPoint(
            x: bounds.width/2,
            y: bounds.height/2 + plusWidth/2 + 0.5))
    }

    //set the stroke color
    UIColor.whiteColor().setStroke()

    //draw the stroke
    plusPath.stroke()

}

}
4

0 回答 0