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

Posted

技术标签:

【中文标题】子类化 UIButton 来做一个圆,最后是一个矩形【英文标题】:Subclassed UIButton to make a circle, end up with a rectangle 【发布时间】:2015-07-13 02:04:26 【问题描述】:

我有以下名为 PushButtonView 的类。它继承了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,然后将类设置为 PushButtonView。只要按钮的宽度和高度相同,按钮就会呈现圆形。

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

    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 直接放在视图上并将类设置为 PushButtonView,那么我最终会得到一个圆形按钮。

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

【问题讨论】:

您是从某处复制并粘贴代码吗?好像你在用它,但你不明白它。 我认为它(至少部分)从这里复制:raywenderlich.com/90690/modern-core-graphics-with-swift-part-1 我将代码放到网上,并对其进行了一些修改。边框和边框颜色。根据下面的答案,我只是搞砸了一行代码。感谢您的回答,Aaron Brager。 【参考方案1】:

我认为你必须设置useBorder:

button1.useBorder = true

否则图层的cornerRadius 不会被设置。

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

【讨论】:

以上是关于子类化 UIButton 来做一个圆,最后是一个矩形的主要内容,如果未能解决你的问题,请参考以下文章

我在 MonoTouch 中将 UIButton 子类化,但底层 ObjC 类仍然是 UIButton

Ios UIButton 与 OpenGL 的子类化

子类化 UIButton 标题字体大小意外更改

我将 UIButton 子类化以创建自己的自定义按钮外观,但它不会重绘所有触摸事件

子类化 UIButton 并将 UILabel 添加到视图层次结构会导致 currentImage 压缩

使用 NSLayoutConstraint 实例化和设置 UIButton 子类的大小