简体   繁体   English

IBInspectable setTitle:UIButton上的forState不起作用

[英]IBInspectable setTitle:forState on UIButton not working

I'm trying to implement a localizable class for UIButtons using live rendering in the Interface Builder. 我正在尝试使用Interface Builder中的实时渲染为UIButtons实现可本地化的类。 This is the code I have so far: 这是我到目前为止的代码:

@IBDesignable class TIFLocalizableButton: UIButton {

    @IBInspectable var localizeString:String = "" {
        didSet {
            #if TARGET_INTERFACE_BUILDER
                var bundle = NSBundle(forClass: self.dynamicType)
                self.setTitle(bundle.localizedStringForKey(self.localizeString, value:"", table: nil), forState: UIControlState.Normal)
            #else
                self.setTitle(self.localizeString.localized(), forState: UIControlState.Normal)
            #endif
        }
    }

}

The layout is correctly updating in the IB, but the text isn't showing up. 布局在IB中正确更新,但文本未显示。 I've created the same implementation with UILabel's which does work: https://github.com/AvdLee/ALLocalizableLabel 我用UILabel创建了相同的实现,它可以工作: https//github.com/AvdLee/ALLocalizableLabel

Any ideas on how to fix this are welcome! 欢迎任何有关如何解决这个问题的想法

UIButton类型应该是自定义然后setTitle工作

At WWDC I was able to ask one of the engineers. 在WWDC,我能够问一位工程师。 Turns out to be fixed in this way: 结果以这种方式修复:

override func setTitle(title: String?, forState state: UIControlState) {

    #if TARGET_INTERFACE_BUILDER
        let bundle = NSBundle(forClass: self.dynamicType)
        super.setTitle(bundle.localizedStringForKey(self.localizeString, value:"", table: nil), forState: UIControlState.Normal)
    #else
        super.setTitle(title, forState: state)
    #endif
}

As the interface builder calls the setTitle multiple times for different states. 由于接口构建器针对不同的状态多次调用setTitle。

它现在按照Xcode 8.3.3工作

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

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