iOS์์ ์ฝ๋๋ก ๋ทฐ ์งค ๋ ์์ฐ์ฑ์ ํฌ๊ฒ ๋์ฌ ์ฃผ์ด ๋ง์ด ์ฐ์ด๊ณ ์๋ SnapKit๊ณผ Then ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์์๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
๐ SnapKit
SnapKit๋ iOS์ OS X ๋ชจ๋์์ ์คํ ๋ ์ด์์์ ์ฝ๊ฒ ํ๊ธฐ ์ํ DSL์ ๋๋ค.
DSL: ํน์ ํ ๋๋ฉ์ธ์ ํนํ๋ ์ธ์ด.
Requirements
- iOS 10.0+ / Mac OS X 10.12+ / tvOS 10.0+
- Xcode 10.0+
- Swift 4.0+
SnapKit ์ฌ์ฉ๋ฒ
SnapKit ์ฌ์ฉํ๊ธฐ ์
labelTimer.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
lblTimer.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.45),
lblTimer.heightAnchor.constraint(equalToConstant: 45),
lblTimer.topAnchor.constraint(equalTo: viewProgress.bottomAnchor, constant: 32),
lblTimer.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])
์ฌ์ฉํ๊ธฐ ์ ์ฝ๋๋ translatesAutoresizingMaskIntoConstraints ๋ฅผ false๋ก ๋ฐ๊ฟ์ฃผ๊ณ ์ถ๊ฐํ Anchor๋ค์ ์ผ์ผ์ด ๋ค activate ํด์ฃผ๋ ๊ตฌ์กฐ์ ๋๋ค.
SnapKit ์ฌ์ฉ ํ
labelTimer.snp.makeConstraints {
$0.width.equalToSuperview().multipliedBy(0.45) // 1
$0.height.equalTo(45) // 2
$0.top.equalTo(viewProgress.snp.bottom).offset(32) // 3
$0.centerX.equalToSuperview() // 4
}
snapkit์ ์ฐ๋ฉด ๋ด๋ถ์ ์ผ๋ก translatesAutoresizingMaskIntoConstraints ๋ฅผ false๋ก ๋ฐ๊ฟ์ฃผ๊ณ activate๋ฅผ ํด์ค๋๋ค.
ํด๋ก์ ๋ฅผ ์ฌ์ฉํ์ฌ ์ข ๋ ์ง๊ด์ ์ด๊ณ ํ๋กํผํฐ๋ ๋ฉ์๋๋ค ์ด๋ฆ์ด๋ ์ฌ์ฉ ๋ฐฉ๋ฒ๋ค์ด ๊ธฐ์กด๋ณด๋ค ๋ ๊ฐ๋จํด์ง ๊ฒ์ ํ์ธ ํ ์ ์์ต๋๋ค
์ฒด์ธ ํํ๋ก ๋์ฑ ๊ฐํธํ๊ฒ ์คํ ๋ ์ด์์ ์ก๊ธฐ
์๋์ ๊ฐ์ด ๊ฐ์ ๋ฉ์๋๋ฅผ ์ฌ๋ฌ ๋ฒ ์ฌ์ฉํ ๊ฒฝ์ฐ์๋
child.snp.makeConstraints {
$0.leading.equalToSuperview()
$0.top.equalToSuperview()
$0.trailing.equalToSuperview()
$0.bottom.equalToSuperview()
}
์๋์ ๊ฐ์ด ํ๋กํผํฐ๋ค์ ์ฒด์ธํํ๋ก ์ฐ๋ฌ์ ์์ฑํ ํ ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ์ฝ๋ ์์ ํจ์ฌ ์ค์ผ ์ ์์ต๋๋ค.
child.snp.makeConstraints { make in
make.leading.top.trailing.bottom.equalToSuperview()
}
๋ ์ถ๊ฐ์ ์ธ ๋ด์ฉ์ ์๋ SnapKit github์ Kodeco ๊ฐ์์์ ํ์ธ ํ ์ ์์ต๋๋ค.
https://github.com/SnapKit/SnapKit
SnapKit for iOS: Constraints in a Snap
๐ Then
Then์ ์คํ์ผ์์ด์ iOS ๊ฐ๋ฐ์ ์ ์์ด๋์ด ๋ง๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ ๋๋ค.
์์ Snapkit์ ์์ฑ๋ ui์ ์คํ ๋ ์ด์์์ ๊ฐ๊ฒฐํ๊ฒ ํ ๋ ์ฐ์ธ๋ค๋ฉด Then์ ui ์ธ์คํด์ค๋ฅผ ์ฒ์ ์์ฑํ ๋ ์ฐ์ ๋๋ค. ํด๋ก์ ๋ฅผ ์ฌ์ฉํด์ ์์ฑํ๊ธฐ ๋๋ฌธ์ ์ข ๋ ์ง๊ด์ ์ด๊ณ ๊น๋ํ๊ฒ ์์ฑํ ์ ์์ด์ ๋ง์ด ์ฐ์ ๋๋ค.
Then ์ฌ์ฉํ๊ธฐ ์ ์ฝ๋
let label: UILabel = {
let label = UILabel()
label.textAlignment = .center
label.textColor = .black
label.text = "Hello, World!"
return label
}()
Then์ ์ฌ์ฉํ ์ฝ๋
let label = UILabel().then {
$0.textAlignment = .center
$0.textColor = .black
$0.text = "Hello, World!"
}
ํ๋ฉด์ ์ฌ๋ฆด UI์ธ์คํด์ค๋ฅผ ์์ฑํ๊ณ ํ๋กํผํฐ๋ค์ ์ค์ ํ ๋ ์ฝ๋ ์์ด ์ค์ด๋ค๊ณ ์ง๊ด์ ์ด๊ณ ๊น๋ํ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค.
๋ ์์ธํ ๋ด์ฉ์ ํ๋จ์ ๋ ํฌ์งํฐ๋ฆฌ์์ ํ์ธ ํ ์ ์์ต๋๋ค.
https://github.com/devxoul/Then
CocoaPod ์ ์ด์ฉํ์ฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์นํ๊ธฐ
๊ทธ๋ผ ์ด์ ์ด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ ์ฌ์ฉํ๊ธฐ ์ํด ํ๋ก์ ํธ์ ์ถ๊ฐํด ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
1. CocoaPod์ด ์๋ค๋ฉด ๋จผ์ ํฐ๋ฏธ๋์์ ์๋์ ๋ช ๋ น์ด๋ก CocoaPod์ ์ค์นํด์ค๋๋ค.
gem install cocoapods
CocoaPods : Swift ๋ฐ Objective-C ์ฝ์ฝ์ ํ๋ก์ ํธ์ ์ข ์์ฑ ๊ด๋ฆฌ์. 96,000๊ฐ ์ด์์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ณด์ ํ๊ณ ์์ผ๋ฉฐ 3๋ฐฑ๋ง ๊ฐ ์ด์์ ์ฑ์์ ์ฌ์ฉ.
2. Snapkit์ ์ฌ์ฉ ํ ํ๋ก์ ํธ .xcodeproj๊ฐ ์๋ ๊ฒฝ๋ก๋ก ์ด๋ํ์ฌ ์๋ ๋ช ๋ น์ด๋ฅผ ์์ฐจ์ ์ผ๋ก ์ ๋ ฅํฉ๋๋ค.
pod init //Podfile ์์ฑ vim Podfile. //Podfile ํธ์ง
3. Podfile์ ํ๋จ์ ์ฝ๋ ๋๋ก snapkit๊ณผ then์ ์ถ๊ฐํฉ๋๋ค. ( i ๋๋ฌ์ ํธ์ง ๋ชจ๋๋ก ์ ํ ํ ํ ์คํธ ์ถ๊ฐ)
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'WorldHunter' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
# Pods for WorldHunter
pod 'SnapKit', '~> 5.6.0'
pod 'Then'
end
4. Podfile ์ ์ฅ ํ ํ pod install (์ ์ฅ : esc ๋๋ฌ์ ํธ์ง๋ชจ๋ ์ข ๋ฃ ํ :wq ์ ๋ ฅ)
๊ธฐ์กด ํ๋ก์ ํธ๋ฅผ ๋ซ๊ณ ๋๊ฐ๋ฉด ์๋ก ์๊ธด xcworkspaceํ์ผ์ด ์๋๋ฐ ์์ผ๋ก๋ ์ด ํ์ผ์ ์ด์ด์ ์์ ์ ํ๋ฉด ๋ฉ๋๋ค.
xcworkspace๋?
- workspace๋ ํ๋ก์ ํธ ๋ฐ ๊ธฐํ ๋ฌธ์๋ฅผ ๊ทธ๋ฃนํํ์ฌ ํจ๊ป ์์ ํ ์ ์๋ Xcode document์ ๋๋ค
- workspace ์ ์ํ๋ ์์ Xcode ํ๋ก์ ํธ ๋ฐ ๋ค๋ฅธ ํ์ผ๋ค์ ํฌํจํ ์ ์์ต๋๋ค
- ๊ฐ Xcode ํ๋ก์ ํธ์ ํ์ผ์ ๊ตฌ์ฑํ๋ ๊ฒ ์ธ์๋ workspace๋ ํฌํจ๋ ํ๋ก์ ํธ์ target ๊ฐ์ ์์์ (implicit) ๋ฐ ๋ช ์์ (explicit) ๊ด๊ณ๋ฅผ ์ ๊ณตํฉ๋๋ค.
์ธ๋ถ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ ์์ด ํ๋ก์ ํธ๋ฅผ ์์ฑํ๊ณ ์ฌ์ฉํ ๊ฒฝ์ฐ์๋ xcodeproj๋ก ์คํํด๋ ๋์ง๋ง, ์ธ๋ถ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ ๊ฒฝ์ฐ์๋ ๋ ์ด์์ ํ๋ก์ ํธ๋ฅผ ๋ชจ์ ํ ๊ตฐ๋ฐ์ ์์ ํ๋ ์ํฉ์ด ๋๋ workspace ์์ ์์ ์ ํด์ผํฉ๋๋ค.
xcworkspace๋ฅผ ์ด๋ฉด ์๋์ ๊ฐ์ด SnapKit๊ณผ Then์ด ์ถ๊ฐ ๋ ๊ฒ์ ํ์ธ ํ ์ ์์ต๋๋ค.
์ํ๋ ํ์ผ์์ import๋ฅผ ํด์ค ๋ค ์ฌ์ฉํ๋ฉด ๋ฉ๋๋ค.
Example
import UIKit
import SnapKit
import Then
class ViewController: UIViewController {
//MARK: Components
private let titleView = UIView()
//Then ์ฌ์ฉํด์ ์ธ์คํด์ค ์์ฑ
private let titleLabel = UILabel().then{
$0.text = "์ฝ์ ์ ์ฒญ"
$0.font = UIFont.hanSansBoldFont(ofSize: 18)
}
private let closeButton = UIButton().then{
$0.setBackgroundImage(UIImage(named: "btn_close"), for: .normal)
$0.addTarget(self, action: #selector(touchUpCloseButton(_:)), for: .touchUpInside)
}
override func viewDidLoad() {
super.viewDidLoad()
setLayout()
}
//MARK: Layout
func setLayout() {
self.navigationController?.isNavigationBarHidden = true
view.addSubview(titleView)
titleView.addSubviews([titleLabel,closeButton])
titleView.snp.makeConstraints{
$0.top.leading.trailing.equalTo(view.safeAreaLayoutGuide)
$0.height.equalTo(58)
}
titleLabel.snp.makeConstraints{
$0.centerX.equalToSuperview()
$0.centerY.equalToSuperview()
}
closeButton.snp.makeConstraints{
$0.trailing.equalToSuperview().offset(-4)
$0.centerY.equalToSuperview()
$0.width.height.equalTo(48)
}
}
@objc func touchUpCloseButton(_ sender: UIButton) {
}
}
Reference
SnapKit for iOS: Constraints in a Snap
SnapKit for iOS: Constraints in a Snap
In this tutorial you’ll learn about SnapKit, a lightweight DSL (domain-specific language) to make Auto Layout and constraints a breeze to work with.
www.kodeco.com
Xcode Workspace
Xcode Workspace A workspace is an Xcode document that groups projects and other documents so you can work on them together. A workspace can contain any number of Xcode projects, plus any other files you want to include. In addition to organizing all the fi
developer.apple.com
'iOS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Xcode] Failed to prepare device for development ์๋ฌ (0) | 2023.03.16 |
---|---|
[iOS] iOS Concurrency - 2. ๋์คํจ์น ํ์ ์ข ๋ฅ์ ํน์ฑ (0) | 2023.03.05 |
[iOS] ์ ์ค์ณ๋ก ๋๋ก ์กฐ์ข ํ๋ AI์ฑ ๋ง๋ค๊ธฐ (1) - ํ๋ก์ ํธ ๊ตฌ์กฐ์ MediaPipe์์ ๊ณ ๋ (0) | 2023.02.22 |
[iOS] pod init ์ค๋ฅ ์ ํด๊ฒฐ๋ฐฉ๋ฒ (0) | 2023.02.17 |
[iOS] ์ปค์คํ ํฐํธ ์ถ๊ฐ ๋ฐฉ๋ฒ (0) | 2023.02.16 |