SnapKit は、iOS と OS X の両方で自動レイアウトを簡単にするための DSL です。
CocoaPodsは Cocoa プロジェクトの依存関係マネージャーです。次のコマンドでインストールできます。
$ gem install cocoapods
SnapKit 4.0.0+ をビルドするには、CocoaPods 1.1.0+ が必要です。
CocoaPods を使用して SnapKit を Xcode プロジェクトに統合するには、以下で指定します。
Podfile
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
target '<Your Target Name>' do
pod 'SnapKit', '~> 5.6.0'
end
次に、次のコマンドを実行します。
$ pod install
Carthageは、依存関係を構築し、バイナリフレームワークを提供する分散型依存関係マネージャーです。
次のコマンドを使用して、自作でカルタゴをインストールできます。
$ brew update
$ brew install carthage
Carthageを使用してSnapKitをXcodeプロジェクトに統合するには、次の方法で指定します。
Cartfile
github "SnapKit/SnapKit" ~> 5.0.0
Run
carthage update
to build the framework and drag the built SnapKit.framework
into your Xcode project.
Swift Package ManagerSwift Package Manager is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.
Xcode 11+ is required to build SnapKit using Swift Package Manager.
To integrate SnapKit into your Xcode project using Swift Package Manager, add it to the dependencies value of your
Package.swift
:
dependencies: [
.package(url: "https://github.com/SnapKit/SnapKit.git", .upToNextMajor(from: "5.0.1"))
]
ManuallyIf you prefer not to use either of the aforementioned dependency managers, you can integrate SnapKit into your project manually.
Usage
Quick Startimport SnapKit
class MyViewController: UIViewController {
lazy var box = UIView()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(box)
box.backgroundColor = .green
box.snp.makeConstraints { (make) -> Void in
make.width.height.equalTo(50)
make.center.equalTo(self.view)
}
}
}
PlaygroundYou can try SnapKit in Playground.
Note:
To try SnapKit in playground, open
SnapKit.xcworkspace
and build SnapKit.framework for any simulator first.
Resources
SnapKit is released under the MIT license. See LICENSE for details.