带有 xib (iOS 7 & 8) 和实时渲染 (iOS 8) 的 iOS 自定义视图
您可以下载这个仓库,并将 CustomViewKit
拖拽到您的项目中。
然后找到 swift 文件 CustomViewLight.swift
,并将 bundleIdentifier return value
替换成您自己的 bundleIdentifier
在您的项目中添加一个 Podfile
touch Podfile
添加 CustomViewKit pod
platform :ios, '8.0'
use_frameworks!
pod 'CustomView', '~> 1.4'
然后创建一个 swift 文件 CustomViewKitExtension.swift
,并重写 bundleIdentifier
方法
重写 setup
方法,并添加您的自定义代码
import UIKit
import CustomView
class CV1: CustomViewLight {
override func setup() {
super.setup()
// custom code
var c = UILabel(frame: CGRectMake(0, 0, 100, 100))
c.text="1000"
self.view?.addSubview(c)
}
}
创建与名称相同的 swift 文件和 xib 文件
import UIKit
import CustomView
class CV2: CustomViewLayerStyle {
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!
@IBOutlet weak var label3: UILabel!
}
为您的 xib 设置 File's Owner
extension ViewStyles {
public func customStyle1() -> ViewStyle{
return ViewStyle().set(borderWidth:20).set(borderColor:UIColor.darkGrayColor()).set(cornorRadius:20)
}
public func customStyle2() -> ViewStyle{
return ViewStyle().set(borderWidth:30).set(borderColor:UIColor.greenColor()).set(cornorRadius:30)
}
}
通过字符串(方法名)设置样式,并且 CustomViewLayerStyle
将通过 KVC (键值编码)找到对应的方法。
var cv2 = CV2(frame:CGRectMake(0, 100, 150, 150))
cv2.style = "customStyle1"
注意:返回类型必须是
ViewSource
extension ViewSources {
public var cv2Source1:ViewSource
{
get {
return YumeViewSource<CV2>(){
(CV2) -> () in
CV2.label1.text = "11111"
CV2.label2.text = "22222"
CV2.label3.text = "33333"
}
}
}
public func cv2Source2() -> ViewSource{
return YumeViewSource<CV2>(){
(CV2) -> () in
CV2.label1.text = "abc"
CV2.label2.text = "123"
CV2.label3.text = "xyz"
}
}
}
var cv2 = CV2(frame:CGRectMake(0, 100, 150, 150))
cv2.viewSource = "cv2Source1"