First straight to the chase: @ibinspectable & @ibdesignable
for @IBInspectable and @IBDesignable See official documentation: Creating a Custom View that renders in Interface Builder
Of course, you can also read the following Chinese version: http://nshipster.cn/ibinspectable-ibdesignable/
If the custom view is written in pure code, the above two processing is simple, just specify the class name.
But if this custom view is written, what if you let the Xib interface render directly to storyboard?
1. Create a Idview and add a idview.xib
2. Adding Constraints to Idcard.xib
3. The File ' s Owner class in Idcard.xib is set to Idcard:
4. Add the following code to the Idcard.swift to connect the Xib view to the Contentview on the code:
5. Bind Xib, implement @IBInspectable, @IBDesignable These parts of the code
@IBDesignableclassIdcard:uiview {@IBOutlet var contentview:uiview!@IBInspectable var cornerradius:cgfloat=0{didset {Layer.cornerradius=Cornerradius layer.maskstobounds= Cornerradius >0}} @IBInspectable var borderwidth:cgfloat=0{didset {layer.borderwidth=BorderWidth}} @IBInspectable var bordercolor:uicolor?{didset {layer.bordercolor= BorderColor?. Cgcolor}}OverrideInit (frame:cgrect) {super.init (frame:frame) initialfromxib ()} required init?(coder Adecoder:nscoder) {super.init (Coder:adecoder) Initialfromxib ()} func initialfromxib () {Let bundle=NSBundle (forClass:self.dynamicType) let nib= Uinib (nibname:"Idcard", Bundle:bundle) Contentview= Nib.instantiatewithowner (self, options:nil) [0] as!UIView Contentview.frame=bounds Addsubview (Contentview)}}
6. Drag into view at Main.storyboard and specify its class as Idcard and constrain it
7. Run the code with the following results
Summarize some of the pits encountered:
Swift's Xib Custom view visualization to Storyboard