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的更多相关文章

  1. Swift - @IBDesignable和@IBInspectable

    前言: 用storyboard/xib搞项目时,一些属性在Interface Builder上时无法设置,比如常用的layer的一些属性cornerRadius,borderColor等 (有时没必须 ...

  2. @IBDesignable和@IBInspectable

    近期一直在看苹果公司提供的两本swift官方教程电子书,一部是<The Swift Programming Language>,还有一部是<Using Swift With Coco ...

  3. swift 第十四课 可视化view: @IBDesignable 、@IBInspectable

    以前应objctiew-c 写项目的时候,就知道有这两个关键字,现在用swift了.用法稍作改变,基本用法还是一致的 虽然使用这个之后,有时候会报错的非常的莫名其妙----(其实还是自己技术不够牛…… ...

  4. ios 关键字 IB_DESIGNABLE IBInspectable 尝鲜

    每次都用代码定义一个属性,然后在viewDidLoad中再去设置这个属性,最常见的就是什么圆角,描边的, 现在终于可以直接像系统的属性一样在界面上设定了 界面上修改你的属性,减少代码压力

  5. 如何设计一个 iOS 控件?(iOS 控件完全解析) (转)

    前言 一个控件从外在特征来说,主要是封装这几点: 交互方式 显示样式 数据使用 对外在特征的封装,能让我们在多种环境下达到 PM 对产品的要求,并且提到代码复用率,使维护工作保持在一个相对较小的范围内 ...

  6. 关于IB_DESIGNABLE / IBInspectable的那些事

    前言 IB_DESIGNABLE / IBInspectable 这两个关键字是在WWDC 2014年”What’s New in Interface Builder”这个Session里面,用Swi ...

  7. 如何设计一个 iOS 控件?(iOS 控件完全解析)

    前言 一个控件从外在特征来说,主要是封装这几点: 交互方式 显示样式 数据使用 对外在特征的封装,能让我们在多种环境下达到 PM 对产品的要求,并且提到代码复用率,使维护工作保持在一个相对较小的范围内 ...

  8. 设计一个 iOS 控件

    转载自:http://blog.csdn.net/zhangao0086/article/details/45622875 代码的等级:可编译.可运行.可测试.可读.可维护.可复用 前言 一个控件从外 ...

  9. iOS 屏幕适配,autoResizing autoLayout和sizeClass图文详解

    === 1. autoResizing autoresizing是苹果早期的ui布局适配的解决办法,iOS6之前完全可以胜任了,因为苹果手机只有3.5寸的屏幕,在加上手机app很少支持横屏,所以iOS ...

随机推荐

  1. jsonp 请求报Uncaught SyntaxError: Unexpected token :

    $(document).ready(function() { jQuery.ajax({ type: 'GET', url: 'http://wncrunners.com/admin/colors.j ...

  2. Leetcode415Add Strings字符串相加

    给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 注意: num1 和num2 的长度都小于 5100. num1 和num2 都只包含数字 0-9. num1 和num2 都不包 ...

  3. Effective Modern C++ 条款4:掌握查看型别推导结果的方法

    采用何种工具来查看型别推导结果,取决于你在软件开发过程的哪个阶段需要该信息.主要研究三个可能的阶段:撰写代码阶段.编译阶段.运行时阶段. IDE编译器 IDE中的代码编译器通常会在你将鼠标指针选停止某 ...

  4. JAVA面试常见问题之进程和线程篇

    1.线程和进程的概念.并行和并发的概念 进程:计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本 ...

  5. Opencv中RGB通道/HSV通道并分离

    OpenCV中HSV颜色模型及颜色分量范围 opencv HSV 颜色模型(H通道取值 && CV_BGR2HSV_FULL) [opencv]在hsv颜色空间识别区域颜色 将原图分离 ...

  6. 移动端适配(rem单位定义方法)

    注:移动端必须写: <meta name="viewport" content="width=device-width, user-scalable=no, ini ...

  7. PHP--Smarty的template模式

    function change_year() { var ss = $('#select_year').children('option:selected').val(); $.ajax({ type ...

  8. day 56

    目录 聚合查询 分组查询 F与Q查询 ORM字段及参数 13个字段操作总结 自定义char字段 ORM中事物的操作 数据库三大范式 聚合查询 aggregate()是QuerySet()的一个终止子句 ...

  9. tcpdump的表达式介绍

    表达式是一个正则表达式,tcpdump利用它作为过滤报文的条件,如果一个报文满足表 达式的条件,则这个报文将会被捕获.如果没有给出任何条件,则网络上所有的信息包 将会被截获. 在表达式中一般如下几种类 ...

  10. ubuntu16.04上在使用搜狗输入法时,按shift不能正常切换中英文

    问题描述: ubuntu16.04上在使用搜狗输入法时,不知道把什么组合键给错按了,导致了按shift不能正常切换中英文,这是一件很烦恼的事儿! 解决步骤: 1,终端输入打开: fcitx-confi ...