这时候就要使用 @IBInspectable 在 IB 面板中添加这些没有的属性。 关于在 OC 中使用 IBInspectable 可以看一下我的 这篇文章 正文 在项目中最常遇到的情况是为 view 设置圆角、描边,以及为 文本控件 添加本地化字符串。 本地化字符串 本地化字符串的解决方法和上面的添加圆角一样 extension UILabel { @IBInspectable var localizedKey: String? text } } } extension UITextField { @IBInspectable var localizedKey: String? 参考 《再看关于 Storyboard 的一些争论》 《@IBDesignable and @IBInspectable in Swift 3》
*/ @property (nonatomic, strong)IBInspectable UIColor *borderColor; /** 可视化设置圆角 */ @property (nonatomic , assign)IBInspectable CGFloat cornerRadius; @end UIView+Border&CornerRadius.m #import "UIView+Border cgColor } } @IBInspectable var borderWidth: CGFloat { get { , assign)IBInspectable CGFloat cornerRadius; @end UIView+Border&CornerRadius.m #import "UIView+Border CGColor } } @IBInspectable var borderWidth: CGFloat { get {
@IBInspectable var integer: Int = 0 @IBInspectable var float: CGFloat = 0 @IBInspectable var double : Double = 0 @IBInspectable var point: CGPoint = CGPointZero @IBInspectable var size: CGSize = CGSizeZero @IBInspectable var customFrame: CGRect = CGRectZero @IBInspectable var color: UIColor = UIColor.clearColor () @IBInspectable var string: String = "" @IBInspectable var bool: Bool = false复制代码 ? IBInspectable实质是在Runtime Attributes设置了值,这也使得IBInspectable只能使用常用类型。NSDate这种类型没法设置成IBInspectable。
开发者也对其进行一些自定义的设置,列举如下: //设置最大值 @property (nonatomic) IBInspectable NSUInteger maximumValue; //设置最小值 @ property (nonatomic) IBInspectable CGFloat minimumValue; //星级视图当前值 @property (nonatomic) IBInspectable ) IBInspectable BOOL allowsHalfStars; //是否是否允许精确选择 可以根据选择位置进行精确 @property (nonatomic) IBInspectable BOOL UIImage *emptyStarImage; //设置半星的图片 @property (nonatomic, strong) IBInspectable UIImage *halfStarImage ; //设置全星时的图片 @property (nonatomic, strong) IBInspectable UIImage *filledStarImage; 专注技术,热爱生活,交流技术,也做朋友
http://nshipster.cn/ibinspectable-ibdesignable/ 我们来说说如何对某个控件进行圆角、描边处理: 初级 对于一个初学者来说,如果要进行某个控件的圆角、描边设置 创建UIView的分类,使用IBInspectable+ IB_DESIGNABLE关键字: #import <UIKit/UIKit.h> IB_DESIGNABLE @interface UIView (Inspectable) @property(nonatomic,assign) IBInspectable CGFloat cornerRadius; @property(nonatomic,assign ) IBInspectable CGFloat borderWidth; @property(nonatomic,assign) IBInspectable UIColor *borderColor; 与IB_DESIGNABLE的使用详情可以参考这篇文章《谈不完美的IBDesignable/IBInspectable可视化效果编程》
策略 ---- 使用@IBInspectable 暴露属性给Xib设置 修改Label边距 使用@IBDesignable 在Xib显示更改边距的Label 行动(代码) ---- @IBDesignable class RadiusLabel: UILabel{ private var padding = UIEdgeInsets.zero @IBInspectable var CGFloat { get { return padding.left } set { padding.left = newValue } } @IBInspectable CGFloat { get { return padding.right } set { padding.right = newValue } } @IBInspectable CGFloat { get { return padding.top } set { padding.top = newValue } } @IBInspectable
高级 创建UIView的分类,使用IBInspectable+ IB_DESIGNABLE关键字: #import <UIKit/UIKit.h> IB_DESIGNABLE @interface UIView (Inspectable) @property(nonatomic,assign) IBInspectable CGFloat cornerRadius; @property(nonatomic ,assign) IBInspectable CGFloat borderWidth; @property(nonatomic,assign) IBInspectable UIColor *borderColor myButton : UIButton @end #import "myButton.h" @implementation myButton @end 只要将button的Class选择该空白类即可 关于IBInspectable 与IB_DESIGNABLE的使用详情可以参考这篇文章《谈不完美的IBDesignable/IBInspectable可视化效果编程》
在.h文件中声明属性 @interface RetabBar : UITabBar @property (nonatomic, strong)IBInspectable UIColor *tabBartintColor ; @end 在这段代码中的“IBInspectable”其实就是充当一个桥梁,将自定义的代码实时渲染到Interface Builder中。
在XCODE中如果您想要将视图类的自定义属性出现在attributes inspector 中的话,您需要在您的自定义属性前面加上IBInspectable 关键字。 比如下面的定义: @property(nonatomic, assign) IBInspectable CGFloat myTop; @property(nonatomic, assign) IBInspectable CGFloat myLeading; @property(nonatomic, assign) IBInspectable CGFloat myBottom; @property(nonatomic, assign) IBInspectable CGFloat myTrailing; 目前支持** IBInspectable关键字的属性只有NSString、BOOL、int、float、double
@IBDesignable extension NSView { @IBInspectable var cornerRadius:Float? 参考资料: IBInspectable和IBDesignable。 我们在 SideMenuItemView写一下这个方法。 使用 CALayer设置背景颜色 我们修改 BaseView的方法如下: @IBInspectable var backgroundColor:NSColor! import AppKit extension NSView { @IBInspectable var backgroundColor:NSColor! extension NSButton { @IBInspectable var titleColor:NSColor?
神秘感增强器:IB_DESIGNABLE和IBInspectable IB_DESIGNABLE的宏的功能就是让SB动态渲染出该类图形化界面; 使用IB_DESIGNABLE的方式,把该宏加在自定义类的前面 Paste_Image.png IBInspectable 就是让SB上出现属性的修改框,可以在SB直接修改: ? Paste_Image.png 5.
用 External Object 重构 VC 只能在于 xib IB 中的关键字总结 Swift @IBAction @IBOutlet @IBDesignable @IBInspectable Objective-C IBAction IBOutlet IB_DESIGNABLE IBInspectable IBOutletCollection(ClassName) @IBDesignalbe @IBInspectable 用 @IBInspectable 修饰的属性会显示在 IB 的 Show the Attributes inspector。 } // 存储属性 @IBInspectable var name: String { get { return objc_getAssociatedObject AssociatedKeys.name, newValue, .OBJC_ASSOCIATION_RETAIN) } } // 计算属性 @IBInspectable
@IBDesignable 和 @IBInspectable 的使用 应用场景:我们使用Xib绑定Custom Class想看一下样子,所见即所得,IBDesignable这个属性即可满足这个需求。 有时候我们想在XIb里直观修改Xib属性,但是Xcode并不全部提供属性修改,可以通过Keypath修改,但是键入太麻烦容易出错,所以可以通过IBInspectable添加自定义属性。 import UIKit @IBDesignable class LayerView: UIView { @IBInspectable var myRaduis:CGFloat = 0.0{
StoryBoard中的热重载,主要就在于关键字IB_DESIGNABLE的使用,而这个关键字,和关键字IBInspectable是一同发布的,我就顺带一起来介绍,而且他们搭配起来使用会更加爽滑,接下来我就通过一个小 所以,对于这种方式,我是比较的嗤之以鼻的,也并不推荐大家直接在这里面写,一点都不友好,还有点傻呼呼的,那有更好的办法吗,当然有,就是关键字 IBInspectable,使用这个关键字,他就可以利用运行时机制 然后在RadiusView.h文件里面,我们可以随便自定义变量名,比如,圆角、边框宽度、边框颜色、阴影颜色、阴影偏移offset,然后把IBInspectable直接放到变量前面。 文件里也是可以的,这个时候我需要说明一下,这个关键字,如果你是在M1芯片的Mac上做开发,你必须把Xcode更新到13.0及以上的版本,否则他是不起作用的,并且会给你报一个错,但是不影响运行,也不影响我使用 IBInspectable 甚至我再绘制一个圆,都是可以的,活学活用,我把绘制圆所需要的变量,定义出来,加上关键字IBInspectable,注释掉其他的代码,如下图。 然后,我先给他们设置几个值。
AdaptW(floatValue) (floatValue*[[UIScreen mainScreen] bounds].size.width/kDesignWidth) #endif 约束适配 利用IBInspectable UIKit.h> NS_ASSUME_NONNULL_BEGIN @interface NSLayoutConstraint (XibScale) @property(nonatomic, assign) IBInspectable
unused_optional_binding | no | no | yes 必须使用定义的 optional binding | valid_ibinspectable | no | no | yes IBInspectable 必须是可变参数 | vertical_parameter_alignment
最好的办法是利用extension和@IBInspectable来做: extension UIView { @IBInspectable var cornerRadius: CGFloat { 0 } get { return layer.cornerRadius } } @IBInspectable layer.borderWidth = newValue } get { return layer.borderWidth } } @IBInspectable : nil } } } 标记为@IBInspectable的属性会显示在 Storyboard 上: ?
本想想用@IBDesignable @IBInspectable两个属性声明圆角可以在StoryBoard修改的,但是一直不成功,查了一下资料只支持子类,不支持扩展。 下面是扩展的代码如下: import UIKit @IBDesignable extension UIView { @IBInspectable var cornerRadius:CGFloat
我们还将把imageName标记为@IBInspectable,这样就可以通过Interface Builder来设置它。 导航到属性检查器,你应该看到 "图像名称 "属性(这代表我们设置为@IBInspectable的imageName字符串!)。在这里输入你想嵌套在视图中的图片名称。 是时候建立和运行了!
orientation; @property(nonatomic,assign) MyLayoutViewOrientation orientation; @property(nonatomic,assign) IBInspectable 这些则可以通过视图的扩展分类: @interface UIView(MyFloatLayoutExt) @property(nonatomic,assign,getter=isReverseFloat) IBInspectable BOOL reverseFloat; @property(nonatomic,assign) IBInspectable BOOL clearFloat; @property(nonatomic , assign) IBInspectable CGFloat weight; @end 来设置。