使用 UIKitChain 框架,您可以在一行代码中创建 UIKit 组件
使用 UIKitChain 框架,您可以在一行代码中创建 UIKit 组件。
安装
可可豆荚
CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它:
$ gem install cocoapods
要使用 CocoaPods 将 UIKitChain 集成到您的 Xcode 项目中,请在 Podfile 中指定它:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '12.0'
use_frameworks!
target '<Your Target Name>' do
pod 'UIKitChain', '~> 1.0.0'
end
然后,运行以下命令:
$ pod install
Swift Package Manager
Swift Package Manager 是一个用于管理 Swift 代码分发的工具。它与 Swift 构建系统集成,以自动执行下载、编译和链接依赖项的过程。
使用 Swift Package Manager 构建 UIKitChain 需要 Xcode 11+。
dependencies: [
.package(url: "https://github.com/MnkGitBox/UIKitChain.git", .upToNextMajor(from: "1.0.0"))
]
Manually
If you prefer not to use either of the aforementioned dependency managers, you can integrate UIKitChain into your project manually.
Usage
Call the method right after initializing the UI element. Then you can configure the element using dot notation and finally call to get your complete view element..chain
.component
- Without UIKitChain
let circleView = UIView()
circleView.backgroundColor = .green
circleView.layer.cornerRadius = 50
circleView.layer.borderColor = UIColor.black.cgColor
circleView.layer.borderWidth = 1
circleView.clipsToBounds = true
circleView.translatesAutoresizingMaskIntoConstraints = false
- With UIKitChain
let circleView = UIView().chain.bgColor(.green).cornerRadius(50).clipToBounds(true).border(.black).activeAutoConstrant(false).component
Add Missing Method or Variable
If there is any missing method or variable, You can add by extending the with the corresponding UIKit element.UIKitChain
extension UIKitChain where Component: UIView {
@discardableResult
func roundView() -> Self {
component.layer.cornerRadius = component.bounds.height/2
return self
}
}
Resources
You can find the original sample code here.
Credits
- Malith Nadeeshan (malithnadeeshan)
License
UIKitChain is released under the MIT license. See LICENSE for details.