iOS @IBDesignable和@IBInspectable
http://www.tuicool.com/articles/JVNRBjY
@IBDesignable和@IBInspectable
最近一直在看苹果公司提供的两本swift官方教程电子书,一部是《The Swift Programming Language》,另一部是《Using Swift With Cocoa and Objective-C》。昨天正好看到第二部电子书的“Writing Swift Classes with Objective-C Behavior”这一节,其中讲述了关于实时渲染这一技术。下面是摘抄的其中一段内容:
“Live Rendering
You can use two different attributes—@IBDesignable and @IBInspectable—to enable live, interactive custom view design in Interface Builder. When you create a custom view that inherits from the UIView class or the NSView class, you can add the @IBDesignable attribute just before the class declaration. After you add the custom view to Interface Builder (by setting the custom class of the view in the inspector pane), Interface Builder renders your view in the canvas. You can also add the @IBInspectable attribute to properties with types compatible with user defined runtime attributes. After you add your custom view to Interface Builder, you can edit these properties in the inspector. SWIFT @IBDesignable
class MyCustomView: UIView {
@IBInspectable var textColor: UIColor
@IBInspectable var iconHeight: CGFloat
/* ... */
}
” 摘录来自: Apple Inc. “Using Swift with Cocoa and Objective-C”。 iBooks. https://itunes.apple.com/cn/book/using-swift-cocoa-objective/id888894773?mt=11
其大意就是说,可以将自定义的代码实时渲染到Interface Builder中。而它们之间的桥梁就是通过两个指令来完成,即@IBDesignable和@IBInspectable。我们通过@IBDesignable告诉Interface Builder这个类可以实时渲染到界面中,但是这个类必须是UIView或者NSView的子类。通过@IBInspectable可以定义动态属性,即可在attribute inspector面板中可视化修改属性值。
话不多说,下面举一个简单的例子,这个例子自定义一个UIView的子类,该子类拥有一个UIButton。
import UIKit @IBDesignable
class MyCustomView: UIView { @IBInspectable var buttonTitleColor: UIColor! // button title color
@IBInspectable var buttonTitle: String! // button title
@IBInspectable var buttonFrame: CGRect! // button frame var myButton: UIButton! override init(frame: CGRect) {
// init stored properties
buttonTitleColor = UIColor.redColor()
buttonTitle = "button title"
buttonFrame = CGRectMake(0, 0, 100, 50) myButton = UIButton(frame: buttonFrame)
myButton.setTitleColor(buttonTitleColor, forState: .Normal)
myButton.setTitle(buttonTitle, forState: .Normal) // call super initializer
super.init(frame: frame) // add button to self
addSubview(myButton) } required init(coder aDecoder: NSCoder) {
// init stored properties
buttonTitleColor = UIColor.redColor()
buttonTitle = "button title"
buttonFrame = CGRectMake(0, 0, 100, 50) myButton = UIButton(frame: buttonFrame)
myButton.setTitleColor(buttonTitleColor, forState: .Normal)
myButton.setTitle(buttonTitle, forState: .Normal) // call super initializer
super.init(coder: aDecoder) // add button to self
addSubview(myButton)
} override func layoutSubviews() {
// refresh button state through attribute inspector
myButton.setTitleColor(buttonTitleColor, forState: .Normal)
myButton.setTitle(buttonTitle, forState: .Normal)
} }
上图:
从图中可以看到,代码实时渲染到IB中了。
另外,我们在attribute inspector中也可以看到,由指令@IBInspectable声明的属性也出现在了面板中,通过修改这些值可以动态改变界面的效果(需要实现layoutSubviews方法)
具体工程一览:
iOS @IBDesignable和@IBInspectable的更多相关文章
- Swift - @IBDesignable和@IBInspectable
前言: 用storyboard/xib搞项目时,一些属性在Interface Builder上时无法设置,比如常用的layer的一些属性cornerRadius,borderColor等 (有时没必须 ...
- @IBDesignable和@IBInspectable
近期一直在看苹果公司提供的两本swift官方教程电子书,一部是<The Swift Programming Language>,还有一部是<Using Swift With Coco ...
- swift 第十四课 可视化view: @IBDesignable 、@IBInspectable
以前应objctiew-c 写项目的时候,就知道有这两个关键字,现在用swift了.用法稍作改变,基本用法还是一致的 虽然使用这个之后,有时候会报错的非常的莫名其妙----(其实还是自己技术不够牛…… ...
- ios 关键字 IB_DESIGNABLE IBInspectable 尝鲜
每次都用代码定义一个属性,然后在viewDidLoad中再去设置这个属性,最常见的就是什么圆角,描边的, 现在终于可以直接像系统的属性一样在界面上设定了 界面上修改你的属性,减少代码压力
- 如何设计一个 iOS 控件?(iOS 控件完全解析) (转)
前言 一个控件从外在特征来说,主要是封装这几点: 交互方式 显示样式 数据使用 对外在特征的封装,能让我们在多种环境下达到 PM 对产品的要求,并且提到代码复用率,使维护工作保持在一个相对较小的范围内 ...
- 关于IB_DESIGNABLE / IBInspectable的那些事
前言 IB_DESIGNABLE / IBInspectable 这两个关键字是在WWDC 2014年”What’s New in Interface Builder”这个Session里面,用Swi ...
- 如何设计一个 iOS 控件?(iOS 控件完全解析)
前言 一个控件从外在特征来说,主要是封装这几点: 交互方式 显示样式 数据使用 对外在特征的封装,能让我们在多种环境下达到 PM 对产品的要求,并且提到代码复用率,使维护工作保持在一个相对较小的范围内 ...
- 设计一个 iOS 控件
转载自:http://blog.csdn.net/zhangao0086/article/details/45622875 代码的等级:可编译.可运行.可测试.可读.可维护.可复用 前言 一个控件从外 ...
- iOS 屏幕适配,autoResizing autoLayout和sizeClass图文详解
=== 1. autoResizing autoresizing是苹果早期的ui布局适配的解决办法,iOS6之前完全可以胜任了,因为苹果手机只有3.5寸的屏幕,在加上手机app很少支持横屏,所以iOS ...
随机推荐
- 利用TensorFlow识别手写的数字---基于两层卷积网络
1 为什么使用卷积神经网络 Softmax回归是一个比较简单的模型,预测的准确率在91%左右,而使用卷积神经网络将预测的准确率提高到99%. 2 卷积网络的流程 3 代码展示 # -*- coding ...
- U盘安装Linux CentOS 6.8 系统
1.插入U盘在服务器中的USB接口: 2.选择启动的U盘进入装系统的页面: 3.选择English,按ok: 4.选UEFI:SanDisk Cruzer Edge 1.26 5.选 us 按 ok ...
- RMQ—ST表
RMQ(Range Minimum/Maximum Query),RMQ是一个求给定范围内最大最小值的问题.我们一般使用st算法来解决这类问题(Sparse Table).这个算法原理不难,主要是各种 ...
- C++ 静态绑定与动态绑定------绝不重新定义继承而来的缺省参数
在了解静态绑定和动态绑定之前,先了解什么是对象的静态类型,什么是对象的动态类型. 对象的静态类型:对象在声明时采用的类型.是在编译器决定的. 对象的动态类型:目前所指对象的类型.是在运行期决定的. 动 ...
- linux上源码安装python
Linux安装Python2.7 以下例子基于python 2.7.9,其他版本同理.# 1.下载python# wget https://www.python.org/ftp/python/2.7. ...
- 【python之路33】开发模式单例模式及简单的服务器请求框架原理
1.单例模式指的是创建单个实例,例如:数据库连接池中包含10个数据库连接,用户访问数据时随机从连接池中拿出一个进行连接,其他用户再次访问时不再创建对象进行连接 #!usr/bin/env python ...
- 20190921-雅礼Day1
#error 此人太蒻无法编译 #include<iostream> main(){} Before 哦…… -O2 T1 序列问题:分块(莫队),树状数组,线段树,分治 离线 or 在线 ...
- javascript go()函数
<a href="javascript:history.go(-1);">返回上一页</a> put this in your input tag < ...
- Linux中管理员用户与普通用户之间的切换
使用su进行用户切换 管理员用户切换至普通用户: su [用户名] 使用su命令从高级别用户切换至低级别用户无需输入密码 普通用户切换至管理员用户: 普通用户切换至管理员用户使用 su - 使用l ...
- linux追加中文字库,解决imagemagick 中文乱码的问题。
Windows下的字体丰富多样,而且显示的工整.漂亮. 所以自己想把windows上的字体移到Ubuntu下来.Windows下字体库的位置为C:\Windows\fonts,这里面包含所有windo ...