自定义按钮仍未在今日扩展中绘制

时间:2015-02-25 15:23:34

标签: ios swift ios8-today-widget today-extension

我在9天前发布了this问题。

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

我有一个自定义按钮,可以在主机应用中正常工作,但在今日扩展中无法完全绘制。

以下是故事板中的内容以及预览看起来如何扭曲:

enter image description here

以及它在iPhone 5s上的实际效果:

enter image description here

我认为这与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()

}

}

0 个答案:

没有答案