测试已测试 | ✗ |
Lang语言 | SwiftSwift |
许可证 | MIT |
发布最后发布 | 2017年7月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 James Kauten 维护。
此 pod 旨在通过减少模板代码和隐藏应该简洁、可重用 UIView 子类的外观构建器细节,简化自定义 IBDesignables 的构建过程。
要运行示例项目,请克隆仓库,并首先从 Example 目录运行 pod install
命令。
XibView 可通过 CocoaPods 获取。要安装它,请将以下行添加到您的 Podfile 中:
pod "XibView"
创建自定义 IBDesignable 的基本工作流程看起来是这样的。
import XibView
@IBDesignable class DummyView: XibView {
}
您会注意到,我们不是从 UIView
继承,而是从 XibView
继承。这样做会将此类直接与同名的 xib 文件关联起来。别担心,XibView 是 UIView 的子类。
@IBDesignable
标志。没有它,外观构建器不会知道这是一个待构建的视图。接下来创建 .xib
文件,您将在其中设计您的自定义视图。非常重要,xib 文件的名称应与将要拥有的类匹配。这是 XibView 机制根据子类名称自动找到其 xib 文件的方式。在我们的示例中,文件将被称为 DummyView.xib
。
import XibView
/// an example of the XibView subclassing pattern
@IBDesignable class DummyView: XibView {
/// some segmented control
@IBOutlet weak var segmentedControl: UISegmentedControl!
/// some button
@IBOutlet weak var button: UIButton!
/// the background color of some button, can be changed
/// in the interface builder
@IBInspectable var buttonBackgroundColor: UIColor? {
get {
return button.backgroundColor
}
set {
button.backgroundColor = newValue
}
}
/// the top switch
@IBOutlet weak var switchTop: UISwitch!
/// the bottom switch
@IBOutlet weak var switchBottom: UISwitch!
}
与单目标应用程序中的方法相比,CocoaPods 处理视图的资源包的方式略有不同。PodXibView
旨在解决所有 UI 构建爱好者的问题。所有步骤都相同,再加上以下额外的最后步骤。
该信息可在您的podspec文件中找到,通常看起来像这样。
s.resource_bundles = {
'SomeBundleName' => ['YourCocoaPodProjectDir/Classes/**/*.{storyboard,xib}']
}
PodXibViewSubclassing
协议,并实现返回第7步中获得的包名的所需函数。苹果在访问控制方面有相当详尽的文档。如果您对框架的新手,或者想知道在您的用例中public或open哪个更适用,请阅读这篇文章(如果您还没有阅读,可能应该阅读)
import XibView
/// An example IBDesignable for use within a published cocoapod
@IBDesignable public class DummyView: PodXibView, PodXibViewSubclassing {
/// Return the name of the bundle that the xib for this class
/// is located in.
/// - returns: the name of the bundle that this view lives in
func getBundleName() -> String {
return "SomeBundleName"
}
}
kautenja,[email protected]
XibView可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。