IBFignable的TextField子类呈现给IB但不呈现给模拟器/设备

时间:2017-06-28 20:04:02

标签: ios swift uibutton uitextfield custom-controls

我创建了一个UITextField的子类,它在文本字段的右侧添加了一个浮动按钮。它在IB中都很好,但由于某种原因,按钮图像不会出现在模拟器或设备上。

使用视图调试器,我可以看到按钮在那里,就在它应该的位置。我甚至可以点击它并让目标动作触发。但是,图像在屏幕上不可见。

我尝试弄乱TextView和按钮的clipsToBounds属性无济于事。

我也知道图像资源正在工作,因为我制作了一个图像视图并将图像设置为相同的图像资源,看起来很好。 TextField的类如下:

@IBDesignable
class ButtonedTextField: UITextField {

    @IBInspectable var buttonImage: UIImage?
    var button: UIButton?
    var buttonAction: (() -> ())?

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

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

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

    @objc private func buttonActionWrap() {
        buttonAction?()
    }

    private func configure() {
        let button = UIButton(type: .custom)
        button.addTarget(self, action: #selector(buttonActionWrap), for: .touchUpInside)
        let height = self.frame.height
        let buttonHeight = height - 10
        button.frame = CGRect(x: self.frame.width - (buttonHeight + 10), y: 5, width: buttonHeight, height: buttonHeight)
        button.setImage(buttonImage, for: .normal)
        self.button = button
        self.addSubview(button)
        bringSubview(toFront: button)
    }
}

有什么想法?

0 个答案:

没有答案