๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS/UI

[iOS] ํ‚ค๋ณด๋“œ๊ฐ€ ํ…์ŠคํŠธํ•„๋“œ๋ฅผ ๊ฐ€๋ฆด๋•Œ ํ•ด๊ฒฐ๋ฒ•(When the keyboard covers text field)

by Fomagran ๐Ÿ’ป 2021. 6. 16.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ๐Ÿ‘Ÿ ์ž…๋‹ˆ๋‹ค!

 

์˜ค๋Š˜์€ ํ…์ŠคํŠธํ•„๋“œ๋ฅผ ๋งŒ๋“ค๋‹ค๋ณด๋ฉด ์ •๋ง ์ž์ฃผ ๋งŒ๋‚˜๋Š” ํ‚ค๋ณด๋“œ ๋ฌธ์ œ์— ๋Œ€ํ•ด์„œ ๋‹ค๋ค„๋ณด๋ ค๊ณ  ํ•ด์š”.

 

์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ๋กœ ๊ฐœ๋ฐœํ•˜๋‹ค๋ณด๋ฉด ํ‚ค๋ณด๋“œ์˜ ์œ„์น˜๋ฅผ ๊ณ ๋ คํ•˜์ง€ ์•Š๊ณ  ์งœ๋‹ค๊ฐ€ ํ‚ค๋ณด๋“œ๊ฐ€ ์˜ฌ๋ผ์˜ค๋Š” ์ˆœ๊ฐ„ 

 

ํ‚ค๋ณด๋“œ ์•„๋ž˜์— ์žˆ๋Š” ํ…์ŠคํŠธํ•„๋“œ๋‚˜ ๋ฒ„ํŠผ๋“ค์ด ๊ฐ€๋ ค์ง€๊ณค ํ•˜๋Š”๋ฐ์š”.

 

์˜ค๋Š˜์€ ์ด๊ฒƒ์— ๋Œ€ํ•œ ํ•ด๊ฒฐ๋ฒ•์„ ์ •๋ฆฌํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”!


Preview

 

 


Storyboard

 

์•„๋ž˜ ํ™”๋ฉด๊ณผ ๊ฐ™์ด ํ…์ŠคํŠธํ•„๋“œ์™€ ๋ฒ„ํŠผ์ด ๋น„๊ต์  ์•„๋ž˜์— ์žˆ๋„๋ก ๋งŒ๋“ค์–ด ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

View์—๋Š” Tap Gesture Recognizer๋ฅผ ๋‹ฌ์•„๋†“๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 


ViewController

 

์Šคํ† ๋ฆฌ๋ณด๋“œ์—์„œ ๋งŒ๋“ค์–ด์ค€ ํ…์ŠคํŠธํ•„๋“œ์™€ ๋ฒ„ํŠผ์„ ์—ฐ๊ฒฐํ•ด์ค๋‹ˆ๋‹ค.

    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var button: UIButton!

Tap Gesture Recognizer๋„ ์—ฐ๊ฒฐ์‹œ์ผœ์ฃผ์–ด ๋ทฐ๋ฅผ ํƒญํ–ˆ์„ ๋•Œ ํ‚ค๋ณด๋“œ๊ฐ€ ๋‚ด๋ ค๊ฐ€๋„๋ก ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

  @IBAction func tapBackgroundView(_ sender: Any) {
        view.endEditing(true)
    }

 

๊ทธ๋ฆฌ๊ณ  ์‹คํ–‰์„ ์‹œ์ผœ์„œ ํ‚ค๋ณด๋“œ๋ฅผ ์˜ฌ๋ ค๋ณด๋ฉด (cmd + k๋ฅผ ๋ˆ„๋ฅด๋ฉด ๋ฉ๋‹ˆ๋‹ค.)

 

์•„๋ž˜์™€ ๊ฐ™์ด ํ™”๋ฉด์„ ๋ฎ์ฃ ?


ํ•ด๊ฒฐ๋ฒ•

 

์ œ๊ฐ€ ์ƒ๊ฐํ•œ ํ•ด๊ฒฐ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

1. ํ‚ค๋ณด๋“œ๊ฐ€ ์‚ฌ์šฉ๋  ๋•Œ๋ฅผ ๊ฐ์ง€ํ•œ๋‹ค

 

2. ํ‚ค๋ณด๋“œ ๋†’์ด๋ฅผ ์ธก์ •ํ•œ๋‹ค.

 

3. ํ‚ค๋ณด๋“œ ๋†’์ด๋งŒํผ ๋ทฐ์˜ y๋ฅผ ์˜ฌ๋ ค์ค€๋‹ค.

 

4. ํ‚ค๋ณด๋“œ๊ฐ€ ๋‚ด๋ ค๊ฐˆ ๋•Œ๋ฅผ ๊ฐ์ง€ํ•œ๋‹ค.

 

5. ํ‚ค๋ณด๋“œ ๋†’์ด๋งŒํผ ๋ทฐ์˜ y๋ฅผ ๋‚ด๋ ค์ค€๋‹ค.

 


1. ํ‚ค๋ณด๋“œ๊ฐ€ ์‚ฌ์šฉ๋  ๋•Œ๋ฅผ ๊ฐ์ง€ํ•œ๋‹ค

 

UIViewController + Extension.swift

 

๋ชจ๋“  ๋ทฐ์ปจํŠธ๋กค๋Ÿฌ์— ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ UIViewController + Extension.swift๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด ํ‚ค๋ณด๋“œ ์˜ต์ €๋ฒ„๋ฅผ ์„ธํŒ…ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

NotificationCenter์™€ UIResponder๋ฅผ ํ™œ์šฉํ•ด์„œ ํ‚ค๋ณด๋“œ๊ฐ€ ์˜ฌ๋ผ์˜ฌ ๋•Œ๋ฅผ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. 

 

import UIKit

extension UIViewController {
    
    func setKeyboardObserver() {
        NotificationCenter.default.addObserver(self, selector: #selector(UIViewController.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    }
}

 

2. ํ‚ค๋ณด๋“œ ๋†’์ด๋ฅผ ์ธก์ •ํ•œ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด ํ‚ค๋ณด๋“œ๊ฐ€ ๋ณด์ผ๋•Œ ํ‚ค๋ณด๋“œ์˜ ๋†’์ด๋ฅผ ๊ณ„์‚ฐํ•ด์ค๋‹ˆ๋‹ค.

 @objc func keyboardWillShow(notification: NSNotification) {
        if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
                let keyboardRectangle = keyboardFrame.cgRectValue
                let keyboardHeight = keyboardRectangle.height
        }
    }

 

3. ํ‚ค๋ณด๋“œ ๋†’์ด๋งŒํผ ๋ทฐ์˜ y๋ฅผ ์˜ฌ๋ ค์ค€๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด ํ˜„์žฌ ๋ทฐ์˜ origin.y์— ํ‚ค๋ณด๋“œ ๋†’์ด๋งŒํผ ๋นผ์ค๋‹ˆ๋‹ค.

  @objc func keyboardWillShow(notification: NSNotification) {
        if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
                let keyboardRectangle = keyboardFrame.cgRectValue
                let keyboardHeight = keyboardRectangle.height
            UIView.animate(withDuration: 1) {
                self.view.window?.frame.origin.y -= keyboardHeight
            }
        }
    }

 

4. ํ‚ค๋ณด๋“œ๊ฐ€ ๋‚ด๋ ค๊ฐˆ ๋•Œ๋ฅผ ๊ฐ์ง€ํ•œ๋‹ค.

 

setKeyboardObserver์— ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚ด๋ ค๊ฐˆ ๋•Œ๋ฅผ ๊ฐ์ง€ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

 

   func setKeyboardNotification() {
      
        NotificationCenter.default.addObserver(self, selector: #selector(UIViewController.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(UIViewController.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object:nil)
        
    }

 

5. ํ‚ค๋ณด๋“œ์˜ ๋†’์ด๋งŒํผ y๋ฅผ ๋‚ด๋ ค์ค€๋‹ค.

 

ํ‚ค๋ณด๋“œ๋ฅผ ๋†’ํž ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋†’์ด๋ฅผ ๊ตฌํ•ด์„œ ํ˜„์žฌ ๋ทฐ์˜ origin.y์— ํ‚ค๋ณด๋“œ ๋†’์ด๋งŒํผ์„ ๋”ํ•ด์ค๋‹ˆ๋‹ค.

 

 @objc func keyboardWillHide(notification: NSNotification) {
        if self.view.window?.frame.origin.y != 0 {
            if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
                    let keyboardRectangle = keyboardFrame.cgRectValue
                    let keyboardHeight = keyboardRectangle.height
                UIView.animate(withDuration: 1) {
                    self.view.window?.frame.origin.y += keyboardHeight
                }
            }
        }
    }

ViewController

 

๋ทฐ์ปจํŠธ๋กค๋Ÿฌ์— ํ‚ค๋ณด๋“œ ์˜ต์ €๋ฒ„๋ฅผ ์„ธํŒ…ํ•ด์„œ ์‹คํ–‰ํ•ด๋ณด๋ฉด 

 

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var button: UIButton!
    
    override func viewDidLoad() {
        setKeyboardObserver()
    }
    @IBAction func tapBackgroundView(_ sender: Any) {
        view.endEditing(true)
    }
}

 

์•„๋ž˜์™€ ๊ฐ™์ด ํ‚ค๋ณด๋“œ๊ฐ€ ์˜ฌ๋ผ์˜ฌ ๋•Œ ๋ทฐ๊ฐ€ ๊ทธ๋งŒํผ ์˜ฌ๋ผ๊ฐ€๊ณ  ๋‚ด๋ ค๊ฐˆ ๋•Œ ๊ทธ๋งŒํผ ๋‚ด๋ ค๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค!

 

 



Source Code

 


์˜ค๋Š˜์€ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ‚ค๋ณด๋“œ๊ฐ€ ํ…์ŠคํŠธํ•„๋“œ๋‚˜ ๋‹ค๋ฅธ ๊ฒƒ๋“ค์„ ๋ฎ์–ด๋ฒ„๋ฆด ๋•Œ์˜ ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค!

ํ˜น์‹œ๋ผ๋„ ๋„์›€์ด ๋˜์…จ๊ฑฐ๋‚˜ ๊ถ๊ธˆํ•˜์‹  ์ ์ด ์žˆ์œผ๋ฉด ์–ธ์ œ๋“  ๋Œ“๊ธ€๋กœ ๋‹ฌ์•„์ฃผ์„ธ์š”!

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€