iOS ) iOS 15 UIButton.ConfigurationUpdateHandler
UIButton.ConfigurationUpdateHandler
: A closure to update the configuration of a button.
๋ฒํผ์ ๊ตฌ์ฑ์ ์ ๋ฐ์ดํธ ํ๊ธฐ ์ํ ํด๋ก์
private let nextButton: UIButton = {
// ๋ฒํผ์ ์์ฑ์ ์ ์ํด์ฃผ๋ ์ฝ๋ ์๋ต
let buttonStateHandler: UIButton.ConfigurationUpdateHandler = { button in
switch button.state {
case .normal:
button.configuration?.background.backgroundColor = .havitPurple
case .disabled:
button.configuration?.background.backgroundColor = .gray002
default:
return
}
}
button.configurationUpdateHandler = buttonStateHandler
return button
}()
๋ฒํผ์ ์ํ์ ๋ง๊ฒ UI๋ฅผ ๋ณ๊ฒฝํด์ฃผ๋ ๋ถ๋ถ์์ ์ฌ์ฉ !
( ์งํํ ํ๋ก์ ํธ์ ์ต์ ๋ฒ์ ์ด iOS 15์์ ๊ณ ๋ คํ์ฌ UIButton.ConfigurationUpdateHandler๋ฅผ ์ฌ์ฉํด ๋ณด์์ต๋๋ค ! )
๐ฅ ์ฐธ๊ณ ํ ๋ธ๋ก๊ทธ์ ์์
In the old day, we can set some of that in state-dependent property setter methods.
With the coming of UIButton.Configuration, Apple goes with a new approach that is more flexible than the previous API.
๊ธฐ์กด : ๋ฒํผ์ ์ํ์ ๋ฐ๋ผ ํ๋์ฉ ์จ์ฃผ๋ ์ ์ ์ธ ๋ฐฉ๋ฒ / ๋ฉ์๋์์ ์ผ๋ถ๋ถ์ ์ค์ ํด์ค !
=> UIButton.Configuration ์ ํตํด ๋ ์ ์ฐํ ์ ๊ทผ ๋ฐฉ์์ ์ฌ์ฉ !
func setTitle(_ title: String?, for state: UIControl.State)
func titleColor(for: UIControl.State) -> UIColor?
func setBackgroundImage(UIImage?, for: UIControl.State)
button.setTitle("Normal", for: .normal)
button.setTitle("Selected", for: .selected)
button.setBackgroundImage(UIImage(named: "foo"), for: .normal)
โจ ๋ฒํผ์ ๋ด๋ถ ์ํ ๋ณ๊ฒฝ์ ๋ฐ๋ฅธ UIButton.ConfigurationUpdateHandler ์ฌ์ฉ ์์
var configuration = UIButton.Configuration.filled()
configuration.baseBackgroundColor = UIColor.systemPink
configuration.buttonSize = .large
let handler: UIButton.ConfigurationUpdateHandler = { button in // 1
switch button.state { // 2
case [.selected, .highlighted]:
button.configuration?.title = "Highlighted Selected"
case .selected:
button.configuration?.title = "Selected"
case .highlighted:
button.configuration?.title = "Highlighted"
case .disabled:
button.configuration?.title = "Disabled"
default:
button.configuration?.title = "Normal"
}
}
let button = UIButton(configuration: configuration, primaryAction: nil)
button.configurationUpdateHandler = handler // 3
let selectedButton = UIButton(configuration: configuration, primaryAction: nil)
selectedButton.isSelected = true
selectedButton.configurationUpdateHandler = handler // 4
let disabledButton = UIButton(configuration: configuration, primaryAction: nil)
disabledButton.isEnabled = false
disabledButton.configurationUpdateHandler = handler // 5
<1> A update handler get called everytime button state changes.
<2> We set different title based on button.state. We can apply to a single state or combination just like we did with .setTitle("Highlighted Selected", for: [.selected, .highlighted]).
<3, 4, 5> We can use the same handler for all buttons.
๋ฒํผ ์ํ๊ฐ ๋ณ๊ฒฝ๋ ๋ ๋ง๋ค ์ ๋ฐ์ดํธ ํธ๋ค๋ฌ ํธ์ถ
๋ฒํผ ์ํ์ ๋ฐ๋ผ ์ ๋ชฉ์ ๋ฐ๊ฟ์ฃผ๊ธฐ !
๋ชจ๋ ๋ฒํผ์ ๋์ผํ ํธ๋ค๋ฌ ์ฌ์ฉ๊ฐ๋ฅ
์์ ๋ณด์ฌ์ค ์์๋ ๋ฒํผ์ ๋ด๋ถ์ํ ๋ณ๊ฒฝ์ ๋ฐ๋ฅธ ์์์์ต๋๋ค !
์ด ๋ฟ๋ง ์๋๋ผ ๋ฒํผ์ด ๋น์ฆ๋์ค ๋ก์ง์ ๋ฐ๋ฅธ ๋ณ๊ฒฝ๊ณผ ๊ฐ์ด ์ธ๋ถ์ํ ๋ณ๊ฒฝ์ ์ ์ํ๋ ๋ถ๋ถ์๋ ์ฝ๊ฒ ๋์ํ ์ ์๋ค๊ณ ํฉ๋๋ค !
์๋์ ๊ฐ์ด Add Item ๋ฒํผ์ ๋๋ ์ ๋ ๋ค๋ฅธ ๋ฒํผ์ ๋๋ฆฌ๋ ํ์๊ฐ ์นด์ดํ ๋์ด ํ์๋๋ค๋์ง.. ์ ๊ฐ์ !
๋ค์์ ๋ ์์๋ณด๋ ๊ฑธ๋ก ..
์ฐธ๊ณ ํ ์ฌ์ดํธ
https://sarunw.com/posts/dynamic-button-configuration/
Dynamic button configuration in iOS 15 | Sarunw
Learn how to change button configuration, e.g., title and color, based on the internal and external changes.
sarunw.com