子类化 UIButton 做一个圆,最后是一个矩形

Subclassed UIButton to make a circle, end up with a rectangle

我有以下 class 名为 PushButtonView。它子classes UIButton

import UIKit

@IBDesignable
class PushButtonView: UIButton {
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
@IBInspectable var fillColor: UIColor = UIColor.greenColor()
@IBInspectable var isAddButton: Bool = true
@IBInspectable var borderColor: UIColor = UIColor.blackColor()
@IBInspectable var useBorder: Bool = false
@IBInspectable var borderWidth: CGFloat = 1.0

override func drawRect(rect: CGRect) {
    var path = UIBezierPath(ovalInRect: rect)
    fillColor.setFill()

    //Path's don't draw anything. To draw the path, fill it.
    path.fill()

    //Set up width and height variables for the plus
    let plusHeight: CGFloat = 3.0
    let plusWidth: CGFloat = min(bounds.width, bounds.height) * 0.6

    //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.
    //Point is the middle of the button - half of the width of the plus.
    plusPath.moveToPoint(CGPoint(x: bounds.width/2 - plusWidth/2 + 0.5, y: bounds.height/2 + 0.5))

    //Add a point to the path at the end of the stroke.
    //Final point is the middle of the button + half of the width of the plus.
    plusPath.addLineToPoint(CGPoint(x: bounds.width/2 + plusWidth/2 + 0.5, y: bounds.height/2 + 0.5))

    if isAddButton {
        //Move the initial point of the path to the start of the vertical stroke.
        //Point is the middle of the button.  Start point is half of the height - half of the width of the plus.
        plusPath.moveToPoint(CGPoint(x: bounds.width/2 + 0.5, y: bounds.height/2 - plusWidth/2 + 0.5))

        //Add a point to the path at the end of the stroke.
        //Final point is the middle of the button.  Start pointis half of the heigh + half of the width of the plus
        plusPath.addLineToPoint(CGPoint(x: bounds.width/2 + 0.5, y: bounds.height/2 + plusWidth/2 + 0.5))
    }

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

    //Stroke the path
    plusPath.stroke()

    if useBorder {
        //Extra code here.  Paints a black border around the button.  In order for it to be round, the
        //button width and height must be the same.  The cornerRadius must be half of the width (or height).
        super.layer.borderColor = borderColor.CGColor
        super.layer.borderWidth = borderWidth
        super.layer.cornerRadius = super.frame.width / 2 + 0.5
    }
}
}

最初,我使用它在视图上创建按钮。我会在视图上放置一个 UIBUtton,然后将 class 设置为 PushButtonView。只要按钮的宽度和高度大小相同,按钮就会呈现出圆形。

现在我正尝试通过在代码中创建按钮来使用此 class。当我这样做时,我最终得到一个方形按钮,而不是圆形按钮。这是创建按钮的代码。

    button1 = PushButtonView(frame: CGRectMake(0, 0, 50, 50))
    button1.frame = CGRectMake(0, 0, 50, 50)
    button1.center = CGPointMake(self.view.frame.width/2, label.center.y - (label.frame.height/2) - 50)
    button1.fillColor = UIColor.greenColor()
    button1.addTarget(self, action: "button1Clicked", forControlEvents: .TouchUpInside)
    self.view.addSubview(button1)

当我运行它的时候,按钮不是圆的;这是一个正方形。

如果我直接在视图上放置一个 UIButton 并将 class 设置为 PushButtonView,那么我最终会得到一个圆形按钮。

以编程方式创建按钮的代码哪里出错了?

我认为你必须设置 useBorder:

button1.useBorder = true

否则图层的 cornerRadius 不会设置。

如果这是正确的,将来,通过在 drawRect 中设置断点并逐步执行您的实现,可能更容易调试。