๊ธฐํƒ€/iOS๐ŸŽ

[iOS/Swift] ๋น„๋ฐ€๋ฒˆํ˜ธ ์ฐฝ์— ๋ˆˆ์•Œ ( ๋น„๋ฐ€๋ฒˆํ˜ธ ๋ณด์ด๊ฒŒ/์•ˆ๋ณด์ด๊ฒŒ) how to make password hidden/shown

yujindonut 2022. 4. 19. 23:01
728x90

 

 

์ด๋ ‡๊ฒŒ ๋ˆˆ์•Œ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋ณด์ด๊ฒŒ ์•ˆ๋ณด์ด๊ฒŒ ์„ค์ •ํ•  ๊ฒƒ์ด๋‹ค!

 

 

1. ๋ฒ„ํŠผ์˜ ํ˜•ํƒœ๋ฅผ ์„ค์ •ํ•ด์ค€๋‹ค 

๊ฐ€์žฅ ํ”ํžˆ ์‚ฌ์šฉ๋˜๋Š” ๋ฒ„ํŠผ ํƒ€์ž…์€ Custom ๋˜๋Š” Systemํƒ€์ž…์ด๊ณ , ํ•„์š”์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ํƒ€์ž…์„ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

var eyeButton = UIButton(type: .custom)

 

 

2. ๋ˆˆ๋™์ž ๋ฒ„ํŠผ์˜ ๊ฐ์ฒด ์ƒ์„ฑ

primaryAction์— ๋ฒ„ํŠผ ํด๋ฆญ์„ ํ•  ๋•Œ์˜ handler๋ฅผ ์„ค์ •ํ•œ๋‹ค.

eyeButton = UIButton.init(primaryAction: UIAction(handler: { [self]_ in
            passwordTextField.isSecureTextEntry.toggle()
            self.eyeButton.isSelected.toggle()
}))

 

#๋ฒˆ์™ธ์„ค๋ช… - primaryAction

primaryAction์€ UIActionํƒ€์ž…์„ ๋ฐ›๋Š”๋‹ค. addTarget์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!

์˜ˆ ) addTarget๋ฒ„์ „
override func viewDidLoad() { 
	super.viewDidLoad() 
    let button = UIButton(type: .custom) 
    button.addTarget(self, action: #selector(buttonDidTap), for: .touchUpInside) 
} 
@objc 
func buttonDidTap() { 
	print("yujindonut") 
}

์ฝ”๋“œ๋กœ UIButton์„ ์ƒ์„ฑํ•˜๊ณ , ํ•ด๋‹น button์„ ํƒญํ• ๋•Œ๋งˆ๋‹ค action(buttonDidTap)์„ ์ง€์ •ํ•ด์คŒ


iOS14์—์„œ๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ button์— actioin์„ ์ค„ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.
let button = UIButton(type: .custom, 
primaryAction: UIAction(handler: { _ in print("Zedd") 
}))โ€‹

action์— closure๋ฅผ ๋ฐ”๋กœ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค!

3. ๋ˆˆ๋™์ž ๋ฒ„ํŠผ ๊พธ๋ฏธ๊ธฐ

var buttonConfiguration = UIButton.Configuration.plain()
buttonConfiguration.imagePadding = 10
buttonConfiguration.baseBackgroundColor = .clear

configure : ํ™˜๊ฒฝ์„ค์ •, ๊ตฌ์„ฑํ•˜๋‹ค (๋ฒ„ํŠผ์„ ๊พธ๋ฉฐ์ค„๋•Œ ์‚ฌ์šฉํ•œ๋‹ค)

imagePadding : ํ…์ŠคํŠธ ํ•„๋“œ ์šฐ์ธก์— ํ„ฐํ”ผํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ„ํŠผ ๊ณต๊ฐ„์„ ๋‘๊ธฐ ์œ„ํ•ด์„œ

baseBackgroundColor : ๋ฒ„ํŠผ ํด๋ฆญ์‹œ ํšŒ์ƒ‰ ๋ฐฐ๊ฒฝ์„ ํˆฌ๋ช…ํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ

๋”๋ณด๊ธฐ

UIButton.configuration์—๋Š” 4๊ฐ€์ง€์˜ static ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•ด์ค€๋‹ค.

์œ„์—์„œ๋ถ€ํ„ฐ plain, gray, tinted, filled๋ฒ„ํŠผ์ด๋‹ค

4. button์— ์ด๋ฏธ์ง€ ์„ค์ •ํ•ด์ฃผ๊ธฐ

eyeButton.setImage(UIImage(named: "password-hidden-icon"), for: .normal)
self.eyeButton.setImage(UIImage(named: "password-shown-icon"), for: .selected)
self.eyeButton.configuration = buttonConfiguration

.normal : ํด๋ฆญ ์•ˆํ–ˆ์„ ๋•Œ

.selected : ํด๋ฆญ ํ–ˆ์„ ๋•Œ

 

5. passwordTextField์— ์˜ค๋ฅธ์ชฝ์— ๋ˆˆ๋™์ž ๋ฒ„ํŠผ ์ง‘์–ด ๋„ฃ๊ธฐ

self.passwordTextField.rightView = eyeButton
self.passwordTextField.rightViewMode = .always

.rightView์— eyeButton ์ง‘์–ด๋„ฃ๊ณ , righViewMode๋ฅผ always ์†์„ฑ์„ ์คŒ์œผ๋กœ์จ, ์˜ค๋ฅธ์ชฝ ์นธ์„ ํ•ญ์ƒ ๋ณด์—ฌ์ค€๋‹ค.

 

 

 

์ถœ์ฒ˜ : ์œคํœ˜ ๋จธ๋ฆฟ์† ( ์œคํœ˜ ์•Œ๋ผ๋ทฐ)

https://gyuios.tistory.com/126

 

iOS) UIButton.Configuration in iOS 15

iOS 15 ์—์„œ UIButton ์— ์ƒˆ๋กœ์ƒ๊ธด UIButton.Configuration ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž! ๋จผ์ €, ๊ฐœ๋ฐœ์ž๋ฌธ์„œ๋ฅผ ๋”ฐ๋ผ๊ฐ€๋ฉด์„œ ๋ฒ„ํŠผ์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž. Creating Buttons โœจ init(frame: CGRect) Creates a new..

gyuios.tistory.com

https://zeddios.tistory.com/1093

 

iOS 14 + ) UIAction closure based UIControl

์•ˆ๋…•ํ•˜์„ธ์š” :) Zedd์ž…๋‹ˆ๋‹ค. ์–ผ๋งˆ์ „ ํŠธ์œ—์„ ๋ณด๋‹ค๊ฐ€ ์•Œ๊ฒŒ๋œ ์‚ฌ์‹ค์ธ๋ฐ์š”. ์ด๋ ‡๊ฒŒ ์ข‹์€ ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ์ƒ๊ธฐ๋‹ค๋‹ˆ...!!!!ํ•˜๋ฉด์„œ ์ข‹์•„ํ–ˆ์–ด์š” XD ์˜ค๋Š˜์€ iOS 14์—์„œ ์ƒˆ๋กœ๋‚˜์˜จ UIControl์˜ ์ƒˆ๋กœ์šด ์ƒ์„ฑ์ž๋ฅผ ๊ณต๋ถ€ํ•ด๋ณด

zeddios.tistory.com

 

728x90