简体   繁体   English

Swift UIButton覆盖,setImage不起作用

[英]Swift UIButton override, setImage is not working

I'm trying to subclass UIButton with my own button class called FlipsSendButton . 我正在尝试使用我自己的名为FlipsSendButton的按钮类FlipsSendButton UIButton The code for this class is listed below. 下面列出了此类的代码。 In it you can see that I am calling setImage to attempt to set the button image. 在其中你可以看到我正在调用setImage来尝试设置按钮图像。 However, after compiling, everything works correctly EXCEPT setting the image view. 但是,编译后,除了设置图像视图外,一切正常。

Am I missing some steps in my code to set the image, or perhaps calling the optional and unwrap operators incorrectly? 我错过了我的代码中的一些步骤来设置图像,或者可能错误地调用可选和解包操作符?

import Foundation

class FlipSendButton : UIButton {

    let bgColor : UIColor

    init(frame aFrame: CGRect, bgColor aColor: UIColor) {
        bgColor = aColor;

        super.init(frame: aFrame)

        addTarget(self, action: Selector("buttonTouched:"), forControlEvents: .TouchUpInside)
        backgroundColor = aColor;

        setImage(UIImage(named: "flips.png") as UIImage?, forState: .Normal)
    }

    required init(coder aDecoder: NSCoder) {
        bgColor = UIColor.lightGrayColor()
        super.init(coder: aDecoder)
    }

    func buttonTouched(sender: UIButton) {
        if backgroundColor == UIColor.lightGrayColor() {
            backgroundColor = bgColor
        } else {
            backgroundColor = UIColor.lightGrayColor()
        }
    }
}

Also, it is worth mentioning that the image flips.png is in my images.xassets folder, if that makes a difference. 另外,值得一提的是图像flips.png在我的images.xassets文件夹中,如果这有所不同。

You forgot the buttonType property. 你忘了buttonType属性。 Besides, you'd better not subclass the UIButton (check out this for the reason). 此外,你最好别子类UIButton (看看这个为理由)。

Instead, You should set the buttonType property to UIButtonTypeCustom when you init the button, some sample code: 相反,您应该在初始化按钮时将buttonType属性设置为UIButtonTypeCustom ,一些示例代码:

var myButton = UIButton.buttonWithType(.UIButtonTypeCustom)
myButton.setImage(UIImage(named: "flips.png") as UIImage?, forState: .Normal)

myButton.addTarget(self, action: Selector("buttonTouched:"), forControlEvents: .TouchUpInside)

func buttonTouched(sender: UIButton) {
    if sender.backgroundColor == UIColor.lightGrayColor() {
        sender.backgroundColor = bgColor
    } else {
        sender.backgroundColor = UIColor.lightGrayColor()
    }
}

-- EDIT -- - 编辑 -

If you want to reuse the style configuration, a better way is to define your own factory method of UIButton . 如果要重用样式配置,更好的方法是定义自己的UIButton 工厂方法 Something like: 就像是:

import Foundation
import UIKit
import ObjectiveC

private var bgColorKey: UInt8 = 0

extension UIButton {

    var bgColor: UIColor {
        get {
            return objc_getAssociatedObject(self, &bgColorKey) as! UIColor
        }
        set (neewColor) {
            objc_setAssociatedObject(self, &bgColorKey, neewColor, .OBJC_ASSOCIATION_RETAIN)
        }
    }

    class func buttonWithFrame(f: CGRect, backgroundColor color: UIColor) -> UIButton {
        let button = UIButton(type: .Custom)
        button.frame = f
        button.backgroundColor = color;
        button.bgColor = color
        button.setImage(UIImage(named: "flips.png") as UIImage?, forState: .Normal)
        button.addTarget(button, action: "buttonTouched:", forControlEvents: .TouchUpInside)
        return button
    }

    func buttonTouched(sender: UIButton) {
        if sender.backgroundColor == UIColor.lightGrayColor() {
            sender.backgroundColor = sender.bgColor
        } else {
            sender.backgroundColor = UIColor.lightGrayColor()
        }
    }
}

Then you can use the button like: 然后你可以使用如下按钮:

var btn = UIButton.buttonWithFrame(CGRectMake(50, 50, 100, 50), backgroundColor: UIColor.greenColor())
self.view.addSubview(btn)

I found this frustrating as well. 我发现这也很令人沮丧。 A fully functional UIButton subclass, that it's only flaw is setImage: . 一个功能齐全的UIButton子类,它唯一的缺陷是setImage: . The easier solution I've found is writing a tiny helper class to set the image from outside: 我发现的更简单的解决方案是编写一个小辅助类来从外部设置图像:

class ButtonImageHelper {

    func setImage(image: UIImage?, forButton:UIButton) {
            forButton.setImage(image, forState: UIControlState.Normal)
    }   
}

This class can be positioned inside your UIButton subclass. 这个类可以放在你的UIButton子类中。
To call it: 打电话给它:
ButtonImageHelper().setImage(yourImage, forButton:self)

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

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