Swift UIButton子类未在设备或模拟器上显示

时间:2015-02-04 17:34:50

标签: ios iphone swift uibutton subclass

我有以下代码并声明了IBDesignable。

DrawRect在Interface Builder中表现得非常好,但在模拟器或设备上完全没有。

有人有什么想法吗?

import UIKit
import QuartzCore

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

        let color2 = UIColor(red: 0.500, green: 0.500, blue: 0.500, alpha: 0.000)

        //// Oval Drawing
        var ovalPath = UIBezierPath(ovalInRect: CGRectMake(frame.minX + floor(frame.width * 0.03571) + 0.5, frame.minY + floor(frame.height * 0.03571) + 0.5, floor(frame.width * 0.96429) - floor(frame.width * 0.03571), floor(frame.height * 0.96429) - floor(frame.height * 0.03571)))
        color2.setFill()
        ovalPath.fill()
        UIColor.whiteColor().setStroke()
        ovalPath.lineWidth = 3
        ovalPath.stroke()


        //// Rectangle Drawing
        let rectanglePath = UIBezierPath(roundedRect: CGRectMake(frame.minX + floor(frame.width * 0.25714 + 0.5), frame.minY + floor(frame.height * 0.34286 + 0.5), floor(frame.width * 0.60000 + 0.5) - floor(frame.width * 0.25714 + 0.5), floor(frame.height * 0.64286 + 0.5) - floor(frame.height * 0.34286 + 0.5)), cornerRadius: 2)
        color2.setFill()
        rectanglePath.fill()
        UIColor.whiteColor().setStroke()
        rectanglePath.lineWidth = 3
        rectanglePath.stroke()


        //// Bezier Drawing
        var bezierPath = UIBezierPath()
        bezierPath.moveToPoint(CGPointMake(frame.minX + 0.61429 * frame.width, frame.minY + 0.50536 * frame.height))
        bezierPath.addLineToPoint(CGPointMake(frame.minX + 0.75714 * frame.width, frame.minY + 0.34286 * frame.height))
        bezierPath.addLineToPoint(CGPointMake(frame.minX + 0.75714 * frame.width, frame.minY + 0.64286 * frame.height))
        bezierPath.addLineToPoint(CGPointMake(frame.minX + 0.61429 * frame.width, frame.minY + 0.50536 * frame.height))
        bezierPath.closePath()
        bezierPath.lineJoinStyle = kCGLineJoinRound;

        color2.setFill()
        bezierPath.fill()
        UIColor.whiteColor().setStroke()
        bezierPath.lineWidth = 3
        bezierPath.stroke()
        //  }

    }

}

3 个答案:

答案 0 :(得分:2)

将引用更改为'框架'到了' rect'它应该在设备和模拟器中绘制。

答案 1 :(得分:0)

您不应该为UIButton子类重写drawRect:。没有必要;您可以使用其各种属性和方法自定义UIButton的外观。你做这件事的原因还不清楚。 (你 这样做,而不是调用super这一事实可能解释了问题;但解决方案不是,请致电super,而是不做它首先。)

答案 2 :(得分:0)

我最近一直试图做类似的事情 - 创建一个UIButton的子类,覆盖drawRect()以绘制一个简单的自定义图标(我正在制作一个播放/停止按钮,在&#34之间交替;每次点按播放"和"停止"

Play Button

Stop Button

也许我对iOS开发还不够了解,但是为了回应其他答案并且older posts在线不鼓励延长UIButton课程,我不明白为什么这样做会是一个坏主意。当然,您可以通过设置自定义背景图像来自定义按钮,但在我看来,这并不像覆盖drawRect一样灵活(更不用说在您想要更改时随时编辑按钮会很麻烦因为你必须编辑一个实际的图像而不是几行代码。

我确定还有其他方法可以达到相同的效果(例如,通过自定义UIView监听触摸事件来实现按钮),但我认为最干净,最简单的方法仍然只是扩展UIButton类。毕竟,它是subclass itself of UIView.

关于你的代码 - 它似乎在我的环境中正常工作,虽然我不得不稍微改变颜色以使其显示在白色背景下(即UIColor.blackColor().setStroke()而不是UIColor.whiteColor().setStroke())。另外,正如@Astrodan所提到的,使用rect代替frame可能是一个好主意,因为它正在传递给你(尽管代码对我来说是frame):

XCode中的实时渲染故事板:

Storyboard in XCode

在iPhone 6模拟器上:

enter image description here