Swift's Xib Custom view visualization to Storyboard

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.