iOS基于storyboard简单封装的控件
前言
由于公司的项目都是本人单人开发的,所以都是使用storyboard
或者xib
搭建的UI,基本上没有使用代码,所以本人为了更好的看到storyboard
或者xib
上的UI的相关,实现所见即所得,因此对UIKit
的基本控件进行了简单的封装。
实现原理
如果要使你自定义的类和属性在storyboard
或者xib
中实时通过设置的值改变状态,就不得不说这两个关键字:@IBDesignable
和@IBInspectable
。在本篇文章中,它们是主角。
-
@IBDesignable
是用于修饰自定义类的。 -
@IBInspectable
是用于修饰自定义属性的。
代码实现
UIView
的扩展示例:
extension UIView {
@IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
@IBInspectable var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set {
layer.borderWidth = newValue
}
}
@IBInspectable var borderColor: UIColor? {
get {
guard let borderColor = layer.borderColor else { return nil }
return UIColor(cgColor: borderColor)
}
set {
layer.borderColor = newValue?.cgColor
}
}
}
代码中已经封装了常用的UI类,基本满足日常开发需要。
值得说明的是
在HXIBLayoutButton
中定义了枚举HXIBButtonLayoutType
。
在HXIBLoadingButton
中定义了枚举HXIBButtonActivityPosition
和HXIBButtonActivityStyle。
由于storyboard
不支持自定义数据类型的属性,所以这些枚举的rawValue
都是String
类型的,我在定义属性时都是定义的String
类型的属性。比如:
@IBInspectable var activityStyle: String = "white" {
didSet {
guard let style = HXIBButtonActivityStyle(rawValue: activityStyle) else { return }
switch style {
case .white:
activityView.activityIndicatorViewStyle = .white
case .gray:
activityView.activityIndicatorViewStyle = .gray
}
}
}
小tips
有些同学虽然都按照要求定义了类和属性,但是在storyboard
中设置了属性之后并没有变化。遇到这样的情况该怎么办呢?很简单:
在storyboard
界面中,点击xcode
上方菜单栏的Editor
--> Refresh All Views
,这样storyboard
就会强制刷新,等它刷新完了之后,你就会发现你所设置的效果就出来了。但是如果你再改数值,xcode
又没反应了,再重复一下上面的步骤,效果又出来了。不过这样操作就太麻烦了吧,有没有一劳永逸的办法呢?当然有啦,只需要点击xcode
上方菜单栏的Editor
--> Automatically Refresh Views
,这时候在该选择前面有个小勾,就表示你选中了,到时候就可以愉快的实时修改实时反馈啦。
总结
如果这篇文章对各位有那么一些些作用,本人就感觉非常高兴了,谢谢。
这里是本文demo
для вас приятный провайдер, джей казино играть бесплатно в какие слоты играть в джой казино
Respect to article author, some fantastic entropy. Hadria Jens Wearing
Pretty! This was an extremely wonderful article. Many thanks for providing these details. Aurelea Kenyon Jarita
You made some decent points there. I did a search on the subject matter and found most guys will consent with your website. Audre Thorvald Studnia
Great, thanks for sharing this blog article. Really looking forward to read more. Really Great. Jolie Parke Sylvie
What a material of un-ambiguity and preserveness of precious familiarity concerning unexpected feelings. Dareen Garry Buckden
Awesome blog article. Really looking forward to read more. Keep writing. Tobye Stanton Torey
Very good article! We will be linking to this great content on our website. Keep up the great writing. Crysta Valentin Toulon
But wanna remark on few general things, The website design and style is perfect, the subject matter is really fantastic : D. Hesther Nobie Arondell
There is certainly a great deal to learn about this topic. I really like all of the points you made. Leann Tristan Ceciley
I love reading through an article that can make people think. Also, many thanks for allowing me to comment. Toinette Joshua Pelletier
Really enjoyed this article post. Thanks Again. Really Great. Melody Ham Theda
Way cool! Some very valid points! I appreciate you penning this write-up and the rest of the site is also really good. Cayla Hillier Shipp
Thank you!!1