[英]@IBDesignable UIView not calling init within storyboard
I have a UIView
that is is @IBDesignable
我有一个@IBDesignable
的UIView
@IBDesignable
class MyView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
sharedInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
sharedInit()
}
private func sharedInit(){
translatesAutoresizingMaskIntoConstraints = false
backgroundColor = .blue
}
}
When I place this a UIView
in the Storyboard, and assigned its class to MyView
in the Identity inspector , the UIView
still has a default background colour.当我将这个UIView
放在 Storyboard 中,并将它的类分配给Identity inspector 中的MyView
时, UIView
仍然具有默认背景颜色。 Why is its background colour not UIColor.blue
in the Storyboard?为什么它的背景颜色不是故事板中的UIColor.blue
? And, how can I make it like this, please?而且,我怎样才能做到这一点,拜托?
Thanks for any help.谢谢你的帮助。
Initializer which initialize this view from storyboard will call on runtime, for updating view within storyboard in compile time you should try to include prepareForInterfaceBuilder
which updates storyboard xib files in compile time.从故事板初始化此视图的初始化程序将在运行时调用,为了在编译时更新故事板中的视图,您应该尝试包含在编译时更新故事板 xib 文件的prepareForInterfaceBuilder
。
I suggest you to do multiple things when you are going to create @IBDesignable classes :我建议你在创建 @IBDesignable 类时做多件事:
@IBDesignable
tag使用@IBDesignable
标签标记类UIView
property with @IBInspectable
, then you will be able to change the value for this property using StoryBoard使用@IBInspectable
标记UIView
属性,然后您将能够使用StoryBoard更改此属性的值willSet
of that property which is observer for changes before the property takes the value, or didSet
after the property received the value.在该属性的willSet
中设置配置代码,该属性在属性取值之前是变化的观察者,或在属性didSet
后didSet
。prepareForInterfaceBuilder()
which is overriding from its super class kind of UIView
在prepareForInterfaceBuilder()
进行额外的设置,它覆盖了它的超类UIView
Simple and easy !简单易行!
Your code should looks like this :您的代码应如下所示:
import UIKit
@IBDesignable
class myView: UIView {
@IBInspectable var storyBoardColor : UIColor = .red {
willSet(myVariableNameToCatch) {
self.backgroundColor = myVariableNameToCatch
}
}
fileprivate func sharedInit(){
translatesAutoresizingMaskIntoConstraints = false
backgroundColor = storyBoardColor
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
sharedInit()
}
}
myView had initial value of red for storyBoardColor and you can change it from storyBoard ;) myView 的 storyBoardColor 的初始值为 red,您可以从 storyBoard 更改它;)
Once you make your view with tag @IBDesignable.使用标签@IBDesignable 创建视图后。 next thing is to set your properties with @IBInspectable.接下来是使用@IBInspectable 设置您的属性。
@IBDesignable
class MyView: UIView {
@IBInspectable var myBackgroundColour: UIColor? = nil {
willSet(v) {
self.backgroundColor = v
}
}
// YOUR EXISTING CODE HERE
}
Now, when you set the MyView as a class name in Identity Inspector .现在,当您在Identity Inspector中将 MyView 设置为类名时。 You will be able to see your inspectable property in Attributes Inspector .您将能够在Attributes Inspector 中看到您的可检查属性。 there you can set the colour and it will be reflected instantly to your custom view.您可以在那里设置颜色,它会立即反映到您的自定义视图中。
I don't see any usefulness to set the background colour with your custom property because, UIView has the default property to set the background colour.我认为使用自定义属性设置背景颜色没有任何用处,因为 UIView 具有设置背景颜色的默认属性。
Hope it helps.希望能帮助到你。
Swift 4.2 tested and working. Swift 4.2 测试和工作。 This is the cleanest solution.这是最干净的解决方案。
In Xcode make sure Editor -> Automatically refresh views
is checked.在 Xcode 中确保选中Editor -> Automatically refresh views
。
import UIKit
@IBDesignable
class BoxView: UIView {
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
layer.borderColor = borderColor?.cgColor
layer.borderWidth = borderWidth
layer.cornerRadius = cornerRadius
}
@IBInspectable var borderColor: UIColor? {
didSet {
layer.borderColor = borderColor?.cgColor
setNeedsLayout()
}
}
@IBInspectable var borderWidth: CGFloat = 0.0 {
didSet {
layer.borderWidth = borderWidth
setNeedsLayout()
}
}
@IBInspectable var cornerRadius: CGFloat = 0.0 {
didSet {
layer.cornerRadius = cornerRadius
setNeedsLayout()
}
}
}
Identity Inspector
select Custom Class -> Class to "BoxView"
.在Identity Inspector
选择Custom Class -> Class to "BoxView"
。Attributes Inspector
set borderColor
, borderWidth
, and borderRadius
.在Attributes Inspector
设置borderColor
、 borderWidth
和borderRadius
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.