[์ฝ๋๋ฒ ์ด์คUI] ์นด์ดํฐ ์ฑ ๊ฐ๋ฐ ๊ฐ์ธ ๊ณผ์ , inset๊ณผ offset
1. ๊ฐ์ ๋ฒํผ์ ์์น๊ฐ ์ ๋๋ก ์กํ์ง ์์.
์๋๋ ์๋ํด๋ดค๋ ํ ์คํธ.
→
.offset(-32) ๋ก ์์ .
2. ์ซ์ ๋์ฐ๋ ๋ผ๋ฒจ์ด ๋ณด์ด์ง ์์.
→ ๋์์ก์ผ๋ ์ซ์ ์ ๋ ฅ์ด ๋์ด ์์ง ์์์ ์ ์ฉ์ด ๋์ง ์์ ๊ฒ์ฒ๋ผ ๋ณด์์.
์ด๊ธฐํ์ ์์น๊ฐ ์ ๋๋ก ์กํ์ง ์์
1. centerX์ ์์น ์ค์ ์ ๋ผ์๋๊ฑฐ๋ผ ์ถ๊ฐํด์ ํด๊ฒฐ.
2. .inset(-60)์ ์์น ์ค์ ์ด ์๋ชป ๋จ
import UIKit
import SnapKit
class ViewController: UIViewController {
let numLabel = UILabel()
var number: Int = 0
let minusButton = UIButton()
let plusButton = UIButton()
let resetButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
configureUI()
}
//UI์ธํ
๋ฉ์๋
private func configureUI(){
view.backgroundColor = . black
minusButton.backgroundColor = .red
minusButton.setTitle("๊ฐ์", for: .normal)
minusButton.setTitleColor(.white, for: .normal)
minusButton.layer.cornerRadius = 8
minusButton.addTarget(self, action: #selector(minusButtonTapped), for: .touchDown)
numLabel.text = "\(number)"
numLabel.textColor = .white
numLabel.font = .boldSystemFont(ofSize: 45)
numLabel.textAlignment = .center
plusButton.backgroundColor = .blue
plusButton.setTitle("์ฆ๊ฐ", for: .normal)
plusButton.setTitleColor(.white, for: .normal)
plusButton.layer.cornerRadius = 8
plusButton.addTarget(self, action: #selector(plusButtonTapped), for: .touchDown)
resetButton.setTitle("์ด๊ธฐํ", for: .normal)
resetButton.setTitleColor(.white, for: .normal)
resetButton.backgroundColor = .gray
resetButton.layer.cornerRadius = 8
resetButton.addTarget(self, action: #selector(resetButtonTapped), for: .touchDown)
//view์ subview๋ก ๋ฃ์ด์ค๋ค.
[minusButton, numLabel, plusButton, resetButton].forEach {view.addSubview($0)}
//์ ์ฝ ์กฐ๊ฑด
minusButton.snp.makeConstraints {
$0.width.equalTo(80)
$0.height.equalTo(30)
$0.centerY.equalTo(numLabel.snp.centerY)
// $0.trailing.equalTo(numLabel.snp.leading).inset(32)
$0.trailing.equalTo(numLabel.snp.leading).offset(-32)
}
numLabel.snp.makeConstraints {
$0.width.equalTo(80)
$0.center.equalToSuperview()
}
plusButton.snp.makeConstraints {
$0.width.equalTo(80)
$0.height.equalTo(30)
$0.centerY.equalTo(numLabel.snp.centerY)
$0.leading.equalTo(numLabel.snp.trailing).offset(-32)
}
resetButton.snp.makeConstraints {
$0.width.equalTo(80)
$0.height.equalTo(30)
$0.centerX.equalToSuperview()
$0.top.equalTo(numLabel.snp.bottom).offset(60)
}
}
@objc
private func minusButtonTapped() {
number -= 1
numLabel.text = "\(number)"
}
@objc
private func plusButtonTapped() {
number += 1
numLabel.text = "\(number)"
}
@objc
private func resetButtonTapped() {
number = 0
numLabel.text = "\(number)"
}
}
[์ฝ๋๋ฒ ์ด์คUI] SnapKit ์ฝ๋๋ก UI ๊ตฌ์ฑ (0) | 2024.06.26 |
---|---|
[์ฝ๋๋ฒ ์ด์คUI] SnapKit๊ณผ NSLayoutConstraint ์ฝ๋ ๋น๊ต (0) | 2024.06.25 |
[ ์ฝ๋๋ฒ ์ด์คUI ] NSLayoutConstraint, Xcode์ ์ด๋ฏธ์ง ๋ฑ๋กํ๊ธฐ (0) | 2024.06.24 |
[Xcode] ์ฝ๋๋ฒ ์ด์คUI_์คํ ๋ฆฌ๋ณด๋ ์ญ์ (0) | 2024.06.23 |
[Xcode] SPM์ผ๋ก SnapKit ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐ (0) | 2024.06.23 |