繁体   English   中英

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

[英]Swift UIButton Subclass not Showing on device or Simulator

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

DrawRect在Interface Builder中显示得很好,但在Simulator或Device上却完全没有。

有人知道为什么吗?

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()
        //  }

    }

}

将对“框架”的引用更改为“矩形”,它应该插入设备和模拟器中。

您不应该为UIButton子类重写drawRect: :。 没有必要; 您可以使用其各种属性和方法来自定义UIButton的外观。 目前尚不清楚为什么要这样做。 (您正在执行而不是调用super的事实可能解释了这个问题;但解决方案不是,请调用super ,但首先不要这样做。)

我最近一直在尝试做类似的事情-创建UIButton的子类,重写drawRect()以便绘制一个简单的自定义图标(我正在制作一个在每个“播放”和“停止”之间切换的播放/停止按钮点击):

播放按钮

停止按钮

也许我对iOS开发的了解还不够,但是由于网上有人回答其他问题和不鼓励扩展UIButton类的老帖子 ,我不明白为什么这不是一个好主意。 当然,您可以通过设置自定义背景图片来自定义按钮,但是我认为这不像覆盖drawRect那样灵活(更不用说drawRect想要更改某些内容时都很难编辑按钮,因为您必须进行编辑而不是几行代码)。

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

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

XCode中的实时渲染情节提要:

XCode中的情节提要

在iPhone 6模拟器上:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM