๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐ŸŽ iOS/iOS Programming

[iOS] ํ‚คํŒจ๋“œ๊ฐ€ ํ™œ์„ฑํ™”๋  ๋•Œ TextField ๊ฐ€๋ฆฌ๋Š” ๋ฌธ์ œ ํ•ด๊ฒฐํ•˜๊ธฐ, ํ‚คํŒจ๋“œ ํ•ธ๋“ค๋งํ•˜๊ธฐ

ํ‚คํŒจ๋“œ๊ฐ€ ํ™œ์„ฑํ™” ๋˜์–ด ์˜ฌ๋ผ์™”์„ ๋•Œ TextField๊ฐ€ ๊ฐ€๋ ค์ง€๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์ž

 

 

 

 

 

๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•๋“ค์ด ์žˆ์ง€๋งŒ, NotificationCenter์„ ์‚ฌ์šฉํ•ด์„œ ํ•ด๊ฒฐํ•ด๋ณด์ž:)

์›ํ•˜๋Š” ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.


PROCESS

TextField์„ ํƒ์‹œ ํ‚คํŒจ๋“œ๊ฐ€ ์˜ฌ๋ผ์˜ด (*์•ˆ์˜ฌ๋ผ์˜ค๋Š” ๊ฒฝ์šฐ : Cmd + Shift + k)

-> NotificationCenter๋ฅผ ํ†ตํ•ด ํ‚คํŒจ๋“œ๊ฐ€ ์˜ฌ๋ผ์˜ด์„ ์•ฑ์ด ์ธ์ง€

-> ๊ฐ€๋ ค์ง„ TextField๊ฐ€ ๋ณด์ด๋„๋ก ํ™”๋ฉด์ด ์˜ฌ๋ผ๊ฐ

-> TextField์— ๊ฐ’ ์ž…๋ ฅ ํ›„ ํ‚คํŒจ๋“œ ๋‚ด๋ฆผ (ํ™”๋ฉด ํ„ฐ์น˜ or Done ํด๋ฆญ)

-> ํ‚คํŒจ๋“œ๊ฐ€ ๋‚ด๋ ค๊ฐ์„ NotificationCenter๋ฅผ ํ†ตํ•ด ์•ฑ์ด ์ธ์ง€

-> ํ™”๋ฉด ์›์ƒ๋ณต๊ท€

 


1.  NotificationCenter  - Observer ๋“ฑ๋ก

ํ‚คํŒจ๋“œ๊ฐ€ ํ™œ์„ฑํ™” ๋˜๋Š”์ง€ ์•ˆ๋˜๋Š”์ง€ ํƒ์ง€ํ•  observer๋ฅผ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•ด ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ViewDidLoad() ์•ˆ์— ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

override func viewDidLoad() {
	super.viewDidLoad()
	textField.delegate = self
    
    // Register Keyboard notifications
    // addObserver๋ฅผ ํ†ตํ•ด ์˜ต์ €๋ฒ„๋ฅผ ์„ค์ •ํ•  ๋Œ€์ƒ์„ ๋ทฐ์ปจํŠธ๋กค๋Ÿฌ ๊ฐ์ฒด(self)๋กœ ์ง€์ •
	NotificationCenter.default.addObserver(self,
                                            selector: #selector(keyboardWillHide(_:)),
                                            name: UIResponder.keyboardWillHideNotification,
                                            object: nil)
	NotificationCenter.default.addObserver(self,
                                            selector: #selector(keyboardWillShow(_:)),
                                            name: UIResponder.keyboardWillShowNotification,
                                            object: nil)
}

 

addObserver์•ˆ์˜ UIResponder.keyboardWillHideNotification์ด๋ผ๋Š” ์ด๋ฆ„์˜ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด,

self(๋ทฐ์ปจํŠธ๋กค๋Ÿฌ ๊ฐ์ฒด)๊ฐ€ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ณ ,

์ฒ˜๋ฆฌํ•  ๋‚ด์šฉ์€ keyboardWillHide(_:)๋ผ๋Š” ํ•จ์ˆ˜์— ๋‹ด๊ฒจ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 


2. ์‹คํ–‰ ํ•จ์ˆ˜ ์ •์˜

ํ•ด๋‹น ์˜ต์ €๋ฒ„(addObserver) ์‹คํ–‰์‹œ ์ฒ˜๋ฆฌํ•  ํ•จ์ˆ˜์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

ํ‚ค๋ณด๋“œ๊ฐ€ ๋‚˜ํƒ€๋‚จ๊ณผ ๋™์‹œ์— ํ™”๋ฉด์ด ์˜ฌ๋ผ๊ฐ€๋Š” keyboardWillShow์™€

ํ‚ค๋ณด๋“œ๊ฐ€ ๋‚ด๋ ค๊ฐ๊ณผ ๋™์‹œ์— ํ™”๋ฉด์ด ์›๋ž˜๋Œ€๋กœ ๋Œ์•„์˜ค๋Š” keyboardWillHide๋ฅผ ์ •์˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

//ํ‚ค๋ณด๋“œ ์˜ฌ๋ผ๊ฐ”๋‹ค๋Š” ์•Œ๋ฆผ์„ ๋ฐ›์œผ๋ฉด ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ
@objc func keyboardWillShow(_ sender:Notification){
        self.view.frame.origin.y = -150
}
//ํ‚ค๋ณด๋“œ ๋‚ด๋ ค๊ฐ”๋‹ค๋Š” ์•Œ๋ฆผ์„ ๋ฐ›์œผ๋ฉด ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ
@objc func keyboardWillHide(_ sender:Notification){
        self.view.frame.origin.y = 0
}
  • keyboardWillShow : view์˜ y์ขŒํ‘œ๋ฅผ 150๋งŒํผ ์˜ฌ๋ ค์ฃผ์–ด TextField๊ฐ€ ๋ณด์ด๊ฒŒ
  • keyboardWillHide : view๋ฅผ ์›๋ž˜์œ„์น˜๋กœ

 

 


3. TextField ์ž…๋ ฅ์ด ๋๋‚ฌ์Œ์„ ์•Œ๋ฆผ

์ž…๋ ฅ์ด ์™„๋ฃŒ๋˜์—ˆ๋‹ค๋Š” ์•Œ๋ฆฌ๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์—๋Š” 2๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

1)  ํ™”๋ฉด ํ„ฐ์น˜ํ•˜์—ฌ ํ‚คํŒจ๋“œ ๋‚ด๋ฆฌ๊ธฐ

ํ‚คํŒจ๋“œ๋ถ€๋ถ„์„ ์ œ์™ธํ•œ ํ™”๋ฉด์„ ํ„ฐ์น˜ํ•˜์—ฌ ์ž…๋ ฅ์ด ์™„๋ฃŒ๋˜์—ˆ์Œ์„ ์•Œ๋ฆฝ๋‹ˆ๋‹ค.

์ด ์ฒ˜๋ฆฌ๋Š” ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. ์›ํ•˜๋Š” ViewController์— ์•„๋ž˜ ์ฝ”๋“œ ์‚ฝ์ž…ํ•˜๋ฉด ํ•ด๊ฒฐ!

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
         self.view.endEditing(true)
         }

 

2) ํ‚คํŒจ๋“œ์˜ Done ๋ฒ„ํŠผ ํด๋ฆญํ•˜์—ฌ ํ‚คํŒจ๋“œ ๋‚ด๋ฆฌ๊ธฐ

์ด ๋™์ž‘๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋จผ์ € UITextFieldDelegate ํ”„๋กœํ† ์ฝœ์„ ์ค€์ˆ˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ viewDidLoad() ์•ˆ์— ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•˜์—ฌ ์—ฐ๊ฒฐ์‹œ์ผœ์ค€ textField์˜ delegate๋ฅผ ์„ค์ •!

textField.delegate = self

delegate๋ฅผ ์„ค์ • ํ›„ Viewcontroller ์•ˆ์— ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•˜๋ฉด,

ํ‚คํŒจ๋“œ์ƒ์˜ Done, Enter ๋“ฑ ์™„๋ฃŒํ‚ค๋ฅผ ํด๋ฆญํ•˜์—ฌ ์ž…๋ ฅ์ด ์™„๋ฃŒ๋˜์—ˆ์Œ์„ ์•Œ๋ฆฝ๋‹ˆ๋‹ค.๐Ÿ‘ป

func textFieldShouldReturn(_ textField: UITextField) -> Bool{
    textField.resignFirstResponder()
    return true
}

 

 

728x90
๋ฐ˜์‘ํ˜•