zoukankan      html  css  js  c++  java
  • 自定义控件设置属性并实时展现并预览在xib中

    关键字:

    // @IBDesignable:实时看到xib设置后的效果

    // @IBInspectable:给xib提供设置属性,可以xib中看到此属性

    场景:

    自定义一个UITextField,并提供borderColor、borderWidth、cornerRadius三个属性;

    要求:这三个属性能够展现在xib中,改变属性值,能实时预览效果。

    1、我们先自定义类:

     1 import UIKit
     2 
     3 // @IBDesignable:实时看到xib设置后的效果
     4 @IBDesignable
     5 class YSTextField: UITextField {
     6     
     7     // @IBInspectable:给xib提供设置属性,可以xib中看到此属性
     8     @IBInspectable var borderColor:UIColor?{
     9         didSet{
    10             self.layer.borderColor = borderColor?.cgColor
    11         }
    12     }
    13     
    14     // @IBInspectable:给xib提供设置属性,可以xib中看到此属性
    15     @IBInspectable var borderWidth:CGFloat = 0{
    16         didSet{
    17             self.layer.borderWidth = borderWidth
    18         }
    19     }
    20     
    21     // @IBInspectable:给xib提供设置属性,可以xib中看到此属性
    22     @IBInspectable var cornerRadius:CGFloat = 0{
    23         didSet{
    24             self.layer.cornerRadius = cornerRadius
    25             self.layer.masksToBounds = true
    26         }
    27     }
    28 }

    2、新建一个xib,拖入一个UITextField,并把其类对应修改为我们刚自定义的类:YSTextField

    3、在属性栏中我们就可以看到我们定义的属性了

  • 相关阅读:
    HDU-2262 Where is the canteen 概率DP,高斯消元
    HDU-4418 Time travel 概率DP,高斯消元
    无人驾驶相关数据集
    C++——编译器运行过程
    C++——Struct 和 Union区别
    常用linux指令
    无人驾驶——定位
    Ubuntu 没有 无线网 RTL8821ce 8111 8186
    无人驾驶之传感器融合算法
    LIN通讯
  • 原文地址:https://www.cnblogs.com/panda1024/p/6216962.html
Copyright © 2011-2022 走看看