suvera-dev ๐Ÿฅฆ

iOS ) iOS 15 UIButton.ConfigurationUpdateHandler ๋ณธ๋ฌธ

iOS

iOS ) iOS 15 UIButton.ConfigurationUpdateHandler

suvera 2022. 2. 5. 10:20

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 ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ๋‹ค๋ฅธ ๋ฒ„ํŠผ์— ๋ˆŒ๋ฆฌ๋Š” ํšŸ์ˆ˜๊ฐ€ ์นด์šดํŒ… ๋˜์–ด ํ‘œ์‹œ๋œ๋‹ค๋˜์ง€.. ์™€ ๊ฐ™์€ ! 

๋‹ค์Œ์— ๋” ์•Œ์•„๋ณด๋Š” ๊ฑธ๋กœ ..

์ถœ์ฒ˜ :&nbsp;https://sarunw.com/posts/dynamic-button-configuration/

 

 

 

 

 

 

์ฐธ๊ณ ํ•œ ์‚ฌ์ดํŠธ 

 

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

 

Comments