IOS SDK detailed ibinspectable and Ib_designable-storyboad dynamic Refresh

Source: Internet
Author: User

Original blog, reproduced please indicate the source
Blog.csdn.net/hello_hwc
Welcome to my ios-sdk detailed column, where you can find a lot of articles about iOS development basics
Http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html

Objective:
In the UI design of the application, if the property can be set in Interface Builder's graphical interface, and the dynamic preview to the effect, that will undoubtedly greatly improve the development efficiency of the application. And Xcode provides a way for us to use ibinspectable and ib_designable

User Defined Rumtime Attributes

With the user Defined Rumtime attributes you can set some values for KVC properties in Interface Builder. For example
Set rounded corners to 50

This way, when you run the emulator, you have the following effect

However, there are obvious drawbacks to this.

Not easy to debug and post-maintenance

Ib_designable

Ib_designable's macro function is to let Xcode dynamically render this kind of graphical interface.
Use the method to add the macro to the front of the custom class

#import <UIKit/UIKit.h>IB_DESIGNABLE@interface IBDesigbableImageview : UIImageView@end

Then, set ImageView to inherit class, and set the fillet

As you can see, the ImageView on the storyboard dynamically refreshes the

Ibinspectable

Enables KVC-enabled properties to be configured in the attribute Inspector.
I'm not familiar with KVC's children's shoes can look at this article
iOS SDK detailed KVC

Add property and set method, if it is an existing class, use category

For example, set the inheritance class for ImageView Cornerradius
Header File Add Property

@property (nonatomicCGFloat cornerRadius;

. m file implementation of the corresponding Set method

-(void)setCornerRadius:(CGFloat)cornerRadius{    _cornerRadius = cornerRadius;    self.layer.cornerRadius = cornerRadius;    self.layer.masksToBounds0?true:false;}

In this way, the attribute inspector will have one more configuration option

By setting this option, you can set the fillet of the layer.
Each time a fillet is set, a rumtime KVC variable is changed in the identity Inspector

However, there is still no dynamic refresh

Dynamic refresh can be achieved by ib_designable mate Ibinspectable

The implementation is simple, which is to add the macro definition to the header file of the custom class. The corresponding class is then set to the custom class.

. h file

#import <UIKit/UIKit.h>IB_DESIGNABLE@interface IBDesigbableImageview : UIImageView@property (nonatomicCGFloat cornerRadius;@end

. m file

#import "IBDesigbableImageview.h"@implementation IBDesigbableImageview-(void)setCornerRadius:(CGFloat)cornerRadius{    _cornerRadius = cornerRadius;    self.layer.cornerRadius = cornerRadius;    self.layer.masksToBounds0?true:false;}@end

The effect is the beginning of the demo

If unable to refresh dynamically, restart Xcode, if not yet refreshed, such as refreshingallviews, it is recommended to open automatically refresh views

Website about this section of the link
Https://developer.apple.com/library/ios/recipes/xcode_help-IB_objects_media/Chapters/CreatingaLiveViewofaCustomObject.html
My GitHub link to this project is welcome to follow this project
Https://github.com/wenchenhuang/IBInspectableAndIBDesignableDemo

CSDN uploaded resources temporarily not updated, to source code of children's shoes directly to github download

IOS SDK detailed ibinspectable and Ib_designable-storyboad dynamic Refresh

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.