Ios IBInspectable setTitle:forState on UIButton不工作
我正在尝试使用Interface Builder中的实时渲染为UIButtons实现一个可本地化的类。 这是我目前掌握的代码:Ios IBInspectable setTitle:forState on UIButton不工作,ios,swift,uibutton,interface-builder,ibinspectable,Ios,Swift,Uibutton,Interface Builder,Ibinspectable,我正在尝试使用Interface Builder中的实时渲染为UIButtons实现一个可本地化的类。 这是我目前掌握的代码: @IBDesignable class TIFLocalizableButton: UIButton { @IBInspectable var localizeString:String = "" { didSet { #if TARGET_INTERFACE_BUILDER var bu
@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
}
}
}
IB中的布局正在正确更新,但文本未显示。我已经用UILabel创建了相同的实现,它可以工作:
欢迎任何关于如何解决此问题的想法 在WWDC,我可以问一位工程师。结果证明是这样固定的:
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
}
由于接口生成器为不同的状态多次调用setTitle。ui按钮类型应该是自定义的,然后setTitle工作它现在根据Xcode 8.3.3工作,使用iOS 10/Xcode 8 beta6上的
目标接口生成器的setTitle
,对我不起作用。相反,我能够通过直接访问按钮的属性(即myButton.labelTitle?.text=myText