简体   繁体   English

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

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

I have the following code and have declared IBDesignable. 我有以下代码,并声明了IBDesignable。

The DrawRect is shown perfectly well in Interface Builder, but not at all in the Simulator or on Device. DrawRect在Interface Builder中显示得很好,但在Simulator或Device上却完全没有。

Does anybody have any ideas why? 有人知道为什么吗?

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

    }

}

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

You should not be overriding drawRect: for a UIButton subclass. 您不应该为UIButton子类重写drawRect: :。 There is no need; 没有必要; you can customize the look of a UIButton using its various properties and methods. 您可以使用其各种属性和方法来自定义UIButton的外观。 It is far from clear why you are doing this. 目前尚不清楚为什么要这样做。 (The fact that you are doing it, and not calling super , probably explains the problem; but the solution is not, call super , but rather, don't do it in the first place.) (您正在执行而不是调用super的事实可能解释了这个问题;但解决方案不是,请调用super ,但首先不要这样做。)

I have been trying to do a similar thing recently - create a subclass of UIButton, overriding drawRect() in order to draw a simple custom icon (I am making a play/stop button that alternates between "Play" and "Stop" on each tap): 我最近一直在尝试做类似的事情-创建UIButton的子类,重写drawRect()以便绘制一个简单的自定义图标(我正在制作一个在每个“播放”和“停止”之间切换的播放/停止按钮点击):

播放按钮

停止按钮

Maybe I don't know enough about iOS development yet, but in response to other answers and older posts online that discourage extending the UIButton class, I don't understand why this would be a bad idea. 也许我对iOS开发的了解还不够,但是由于网上有人回答其他问题和不鼓励扩展UIButton类的老帖子 ,我不明白为什么这不是一个好主意。 Of course you could customize a button by setting custom background images, but in my opinion this isn't as flexible as overriding drawRect (not to mention it can be a pain to edit your button anytime you want to change something because you have to edit an actual image instead of a few lines of code). 当然,您可以通过设置自定义背景图片来自定义按钮,但是我认为这不像覆盖drawRect那样灵活(更不用说drawRect想要更改某些内容时都很难编辑按钮,因为您必须进行编辑而不是几行代码)。

I'm sure there are other ways to achieve the same result (implement a button via a custom UIView listening for touch events, for example), but I think the cleanest and easiest way is still just to extend the UIButton class. 我确信还有其他方法可以达到相同的结果(例如,通过自定义的UIView监听触摸事件来实现按钮),但是我认为最干净,最简单的方法仍然只是扩展UIButton类。 After all, it is a subclass itself of UIView . 毕竟,它本身是UIView子类。

Regarding your code - it appears to work correctly in my environment, although I had to change the colors slightly to get it to show up against a white background (ie UIColor.blackColor().setStroke() instead of UIColor.whiteColor().setStroke() ). 关于您的代码-尽管我必须稍微更改颜色以使其在白色背景下显示(即UIColor.blackColor().setStroke()而不是UIColor.whiteColor().setStroke() ,但它在我的环境中似乎可以正常工作UIColor.whiteColor().setStroke() )。 Also, as @Astrodan mentioned, it might be a good idea to use rect instead of frame , since it is being passed to you (although the code worked with frame for me): 另外,正如@Astrodan所提到的,使用rect而不是frame可能是一个好主意,因为它已传递给您(尽管代码对我而言适用于frame ):

Live-rendered Storyboard in XCode: XCode中的实时渲染情节提要:

XCode中的情节提要

On iPhone 6 Simulator: 在iPhone 6模拟器上:

在此处输入图片说明

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

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