zoukankan      html  css  js  c++  java
  • ios 在storyboard 和 xib中,显示自定义view的预览效果

    发现FSCalendar这个控件能在xib中显示预览效果,是怎么实现的呢?其中涉及的知识又有哪些?

    主要就是IBInspectable 和 IB_DESIGNABLE

    先看 IBInspectable,来个小demo

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController
    
    @property (nonatomic) IBInspectable UIColor *backgroudColor;
    
    
    @end

    这个IBInspectable,就像IBOutlet,能让interface builder 发现这个属性,比较简单,效果如下图,多了一个background color的选项:

    再来看看IB_DESIGNABLE,用法也比较简单,但是注意,想让xib 绘制出效果,就要重写 initWithFrame 这个函数(initWithCoder是不对的)。

    #import <UIKit/UIKit.h>
    
    IB_DESIGNABLE
    
    @interface IBDesignTestView : UIView
    
    @end
    #import "IBDesignTestView.h"
    
    @implementation IBDesignTestView
    
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            UIButton *testBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
            testBtn.backgroundColor = [UIColor orangeColor];
            [testBtn setTitle:@"testbtn" forState:UIControlStateNormal];
            [self addSubview:testBtn];
        }
        return self;
    }
    
    // 这个函数是专门为 xib设计的,会在渲染前设置你想要配置的属性。
    - (void)prepareForInterfaceBuilder
    {
    self.backgroundColor
    = [UIColor blueColor]; } @end

  • 相关阅读:
    Service Discovery
    Spring security框架原理
    Redis作者谈Redis应用场景
    redis持久化RDB和AOF-转载
    MongoDB树形结构表示法
    Tomcat Connector
    ActiveMQ 负载均衡与高可用(转载)
    JS选取DOM元素的方法
    IObit Driver Booster 无法更新驱动的解决办法
    python 学习备忘
  • 原文地址:https://www.cnblogs.com/breezemist/p/5455801.html
Copyright © 2011-2022 走看看