UIButton Configuration
iOS 15๋ก ์
๋ฐ์ดํธ ์ดํ๋ก UIButton์ ์ ์ํ๋ ๋ฐฉ๋ฒ์ด ๋ค์ํด์ก๋ค.
์
๋ฐ์ดํธ ๋ ์คํ์ผ์ ์ฑํํ์ฌ ์ธํฐํ์ด์ค์ ์์ฐ์ค๋ฝ๊ฒ ์ด์ธ๋ฆฌ๋ ๋ฒํผ์ ๋ง๋ค ์ ์๊ฒ ๋์๋ค.
๊ธฐ์กด์ UIButton์ .setTitle / .setImage ๋ฑ์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ ๋ฒํผ์ ๋ชจ์์ ๋์์ธํ์ง๋ง,
iOS 15์์ ์ถ๊ฐ๋ UIButton.Configuration์ ๊ธฐ๋ณธ ๊ตฌ์ฑ์ ์กฐ๊ธ ๋ ์ฝ๊ณ , ๋ค์ํ๊ฒ ๋ง๋ค ์ ์๋๋ก ๋์์ค๋ค.
configuration์ ํฌ๊ฒ 4๊ฐ์ง๊ฐ ์๋ค.
์คํ ๋ฆฌ๋ณด๋์์ ๋ฒํผ์ ์์ฑํ ๋, ๋ณด๊ณ ๊ทธ๋ฅ ์ง๋์ณค๋ ๋ค๋ฅธ ๋ฒํผ๋ค์ด ์์ ๊ฒ์ด๋ค.
1. plain (๋ฐฐ๊ฒฝ์ด ํฌ๋ช ํ ๋ฒํผ)
static func plain() -> UIButton.Configuration
→ Creates a configuration for a button with a transparent background.
2. Gray (ํ์ ๋ฐฐ๊ฒฝ์ ๋ฒํผ)
static func gray() -> UIButton.Configuration
→ Creates a configuration for a button with a gray background.
3. Tinted (์์กฐ๊ฐ ์๋ ๋ฐฐ๊ฒฝ์์ผ๋ก ๊ตฌ์ฑ๋ ๋ฒํผ)
static func tinted() -> UIButton.Configuration
→ Creates a configuration for a button with a tinted background color.
4. Filled (์์กฐ ์์์ผ๋ก ๋ฐฐ๊ฒฝ์ด ์ฑ์์ง ๋ฒํผ)
static func filled() -> UIButton.Configuration
→ Creates a configuration for a button with background filled with the button's tint color.
์ด์ธ์๋ ์๋์ ๊ฐ์ด ๋ช๊ฐ์ง ๊ตฌ์ฑ๋ค์ด ๋ ์๋ค.
static func borderless() -> UIButton.Configuration
// Creates a configuration for a button that has a borderless style.
// ํ
๋๋ฆฌ ์๋ ์คํ์ผ์ ๋ฒํผ
static func bordered() -> UIButton.Configuration
// Creates a configuration for a button that has a bordered style.
// ํ
๋๋ฆฌ ์คํ์ผ์ด ์๋ ๋ฒํผ
static func borderedTinted() -> UIButton.Configuration
// Creates a configuration for a button that has a tinted, bordered style.
// ์์กฐ์ ํ
๋๋ฆฌ ์คํ์ผ์ด ์๋ ๋ฒํผ
static func borderedProminent() -> UIButton.Configuration
// Creates a configuration for a button that has a prominent, bordered style.
// ๋์ ์ ๋๋ ํ
๋๋ฆฌ ์คํ์ผ์ ๋ฒํผ
์์ ๊ฐ์ด ๋ฒํผ ๋ฐฐ๊ฒฝ ์์ฑ์ ํฌํจํด์ ๋ฒํผ์ ๋ค์ด๊ฐ Title(์ ๋ชฉ)๋ฑ์ ๋ํ ๊ตฌ์ฑ, ์ด๋ฏธ์ง ๊ตฌ์ฑ, ๋ฒํผ์ ์ฝํ
์ธ ์์ญ์์์ ๋ ์ด์์ ๊ตฌ์ฑ ๋ฑ์ configuration ์คํ์ผ์ ์ฑํํด์ ์๋ก์ด ๋ฒํผ ๋์๊ณผ ๋ชจ์์ ์กฐ์ ํ ์ ์๋ค.
๋ฐ๋ก configuration ๊ตฌ์กฐ์ฒด๋ฅผ ํ์ฅํด์ ๋ฒํผ์ ํ๋ฒ ๋ง๋ค์ด๋ณด๋ ค๊ณ ํ๋ค.
extension UIButton.Configuration {
static func yegrStyle() -> UIButton.Configuration {
var configuration = UIButton.Configuration.filled()
configuration.title = "Swift"
configuration.subtitle = "์๋ธํ์ดํ ์
๋๋ค์"
configuration.titleAlignment = .center
configuration.baseBackgroundColor = .systemGreen // ๋ฒํผ ๋ฐฐ๊ฒฝ์
configuration.baseForegroundColor = .white // title ๋ฐ image ์์
configuration.titlePadding = 5 // title ์ฌ์ด ๊ฐ๊ฒฉ
configuration.image = UIImage(systemName: "leaf.fill")
configuration.imagePlacement = .trailing // ์ด๋ฏธ์ง ์์น
configuration.imagePadding = 8
configuration.cornerStyle = .capsule // capsule, dynamic, fixed, large, medium, small
return configuration
}
}
์๋์ ๊ฐ์ด ์ด๋ฏธ์ง ๋์ indicator ์คํ์ผ๋ฅผ ์ถ๊ฐํ ์๋ ์๋ค.
configuration.activityIndicatorColorTransformer = .grayscale // grayscale, monochromeTint, preferredTint
configuration.showsActivityIndicator = true
grayscale, preferredTint ๋์ ์ฐจ์ด๋ ์ ๋ชจ๋ฅด๊ฒ ๋ค.. ๋๋ค white ์์ indicator..?
monochromeTint๋ darkgray? black ์์์ด๋ค.
์ด ๋์์ธ์ ๋ฐ์ดํฐ ๋ฐ์์ค๋ ์๋๊ฐ ๋๋ฆฐ ์ํ์ด๊ฑฐ๋, ์ฌ์ฉ์์๊ฒ ์งํ์ค์ธ ์ํ๋ฅผ ์๋ ค์ค์ผํ ๋(?) ์ฌ์ฉํ๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค.
์ด ๋ฐ์๋ ์ฌ๋ฌ๊ฐ์ง configuration์ด ์์ผ๋, ํ๋์ฉ ๋๋ฌ์ ์ค์ ํด๊ฐ๋ฉด์ '์ด๋ฐ๊ฒ ์๊ตฌ๋~'ํ๊ณ ์์๋๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค.
์์ ๊ฐ์ด extension์ผ๋ก ์ํ๋ ์คํ์ผ๋๋ก ์ค์ ํ ํ,
import UIKit
import SnapKit
class ViewController: UIViewController {
let swiftButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
configureUI()
}
func configureUI() {
view.backgroundColor = .systemGray5
view.addSubview(swiftButton)
let safeArea = view.safeAreaLayoutGuide
swiftButton.snp.makeConstraints {
$0.top.equalTo(safeArea).offset(200)
$0.horizontalEdges.equalTo(safeArea).inset(80)
$0.height.equalTo(50)
}
swiftButton.configuration = .yegrStyle()
}
}
๋ฒํผ์ด๋ฆ.configuration = ๋ง๋ค์๋ UIButton.Configuration
์ด๋ ๊ฒ ํด์ฃผ๋ฉด! ์๋ ํ๋ฉด์ฒ๋ผ ๋ฒํผ์ด ์์ฑ๋ ๊ฑธ ํ์ธํ ์ ์๋ค.
'Swift > Xcode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] CoreData ํ์ฉํด๋ณด๊ธฐ (Subclass ์์ฑ๋ฒ, Double ํ์ ์ ์ธ ์๋๋ ์ด์ ) (0) | 2024.12.12 |
---|---|
[Swift] CLLocationManager (0) | 2024.05.31 |
[Swift] View Drawing Cycle (0) | 2024.05.28 |
[Swift] Delegate ํจํด (0) | 2024.05.26 |
[Swift] UserDefaults (0) | 2024.05.25 |