2

我创建了一个看起来像复选标记的 UIButton 子类。

这是课程:

import UIKit

@IBDesignable
class CheckedButton: UIButton {

    // MARK: - Properties
    @IBInspectable var checked: Bool = false {
        didSet {
            // Toggle the check/uncheck images
            updateImage()
        }
    }


    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }

    private func setup() {
        updateImage()
        self.addTarget(self, action: #selector(tapped), for: .touchUpInside)
    }

    private func updateImage() {
        let image = checked ? UIImage(named: "checked") : UIImage(named: "unchecked")
        self.setImage(image, for: .normal)
    }

    /// Called each time the button is tapped, and toggles the checked property
    @objc private func tapped() {
        checked = !checked
        print("New value: \(checked)")
    }  
}

由于我将checked属性设置为@IBInspectable,因此我在 IB 中看到了它: IBInspectable

奇怪的是:

  • 如果我让这个属性为default,它在情节提要中正确显示

取消选中

  • 但如果我选择其中一个onoff在检查器中选择,则屏幕不会正确更新。

空的

由于该类被标记为@IBDesignable,我希望按钮外观根据在检查器选项卡中为此属性设置的值在 IB 中更新。有线索吗?

4

2 回答 2

4

UIimage(named:) 方法使用主包,但 Interface Builder 以不同的方式加载资源。

试试这个:

UIImage(named: "checked", in: bundle, compatibleWith: nil)

 @IBDesignable

 class CheckedButton: UIButton {

 // MARK: - Properties
 @IBInspectable var checked: Bool = false {
     didSet {
         // Toggle the check/uncheck images
         updateImage()
     }
 }


 override init(frame: CGRect) {
     super.init(frame: frame)
     setup()
 }

 required init?(coder aDecoder: NSCoder) {
     super.init(coder: aDecoder)
     setup()
 }

 override func prepareForInterfaceBuilder() {
     super.prepareForInterfaceBuilder()
     setup()
 }

 internal func setup() {
     self.addTarget(self, action: #selector(tapped), for: .touchUpInside)
 }

 private func updateImage() {
     let bundle = Bundle(for: CheckedButton.self)
     let image = checked ? UIImage(named: "checked", in: bundle, compatibleWith:nil) : UIImage(named: "unchecked", in: bundle, compatibleWith:nil)
     self.setBackgroundImage(image, for: .normal)
 }

 /// Called each time the button is tapped, and toggles the checked property
 @objc private func tapped() {
     checked = !checked
     print("New value: \(checked)")
 }

像这样

于 2017-11-24T16:42:29.643 回答
-1

我的建议是使用其他自定义函数从资产中获取图像:

extension UIImage {
    public static func image(name: String, _ sender: UIView?) -> UIImage? {
        #if TARGET_INTERFACE_BUILDER
            if let sender = sender {
                let bundle = Bundle(for: sender.classForCoder)
                return UIImage(named: name, in: bundle, compatibleWith: sender.traitCollection)
            } else {
                return UIImage(named: name)
            }
        #else
            return UIImage(named: name)
        #endif
    }
}

对于您的示例:

private func updateImage() {
    let image = checked ? UIImage.image(name: "checked", self) : UIImage(name: "unchecked", self)
   setImage(image, for: .normal)
}

此外, UIButton 类具有检查状态,您可以在不创建自己的子类的情况下使用它:

let btn = UIButton(type: .custom)
btn.setImage(UIImage.image(name: "unchecked", self), for: .normal)
btn.setImage(UIImage.image(name: "checked", self), for: .selected)

btn.isSelected = true // change button state between checked and unchecked
于 2017-11-17T16:51:34.720 回答