IBInspectable and IB_DESIGNABLE-Storyboad Dynamic Refresh

Source: Internet
Author: User

IBInspectable and IB_DESIGNABLE-Storyboad Dynamic Refresh

 

Preface:
During the uidesign of the application, if the attribute can be set on the GUI of the Interface Builder and the effect is dynamically previewed, the development efficiency of the application will be greatly improved. XCode provides us with the following method: IBInspectable and IB_DESIGNABLE.

User Defined Rumtime Attributes

You can use User Defined Rumtime Attributes to set values of some KVC Attributes in Interface Builder. For example
Set rounded corner to 50

In this way, running the simulator will have the following effect:

However, this has obvious drawbacks.

Not easy to debug and post-Maintenance

IB_DESIGNABLE

The Macro function of IB_DESIGNABLE is to allow XCode to dynamically render this type of graphical interface.
Add the macro to the front of the custom class.

#import 
  
   IB_DESIGNABLE@interface IBDesigbableImageview : UIImageView@end
  

Then, set imageview as the inheritance class and set the rounded corner.

We can see that the imageview on the storyboard is dynamically refreshed.

IBInspectable

Enables attributes that support KVC to be configured in Attribute Inspector.
If you are not familiar with KVC, read this article.
KVC for ios SDK

Add the attribute and Set method. If it is an existing class, use Category.

For example, set cornerRadius for the inheritance class of imageView
Add attributes to header files

@property (nonatomic) IBInspectable CGFloat cornerRadius;

. M file implementation corresponding set Method

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

In this way, the Attribute Inspector will have an additional configuration option.

By setting this option, you can set the layer rounded corner.
Each time you set a rounded corner, a rumtime KVC variable is changed in Identity Inspector.

However, it still cannot be refreshed dynamically.

You can use IB_DESIGNABLE and IBInspectable to dynamically refresh data.

The implementation method is very simple. You just need to add this macro definition to the header file of the custom class. Set the corresponding class to a custom class.

. H file

#import 
  
   IB_DESIGNABLE@interface IBDesigbableImageview : UIImageView@property (nonatomic) IBInspectable CGFloat cornerRadius;@end
  

. M file

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

The effect is the initial Demo.

If you cannot Refresh the page dynamically, restart XCode. If you cannot Refresh the page, for example, RefreshingAllViews, we recommend that you enable Automatically Refresh Views.

 

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.