Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move view with keyboard using Swift

I have an app that has a text field on the lower half of the view. This means that when I go to type in the text field the keyboard covers the textfield.

How would I go about moving the view upwards while typing so I can see what i'm typing and then moving it back down to its original place when the keyboard disappears?

I've looked everywhere but all the solutions appear to be in Obj-C which I can't quite convert just yet.

Any help would be greatly appreciated.

like image 677
Alex Catchpole Avatar asked Sep 27 '14 00:09

Alex Catchpole


People also ask

How do I move the view up on keyboard in IOS?

The best way to do this is to place your content inside a UIScrollView, then adjust the scroll view's contentInset property by the height of the keyboard when it's shown. Absolutely do not assume the keyboard height--use the value from the "keyboard will show" notification.

How do I push up when a keyboard appears SwiftUI?

Moving SwiftUI View Up When Keyboard AppearsCreate keyboard height state. SwiftUI will automatically update the view whenever the keyboard height changes. Add padding to the bottom of the view, which will make it move up and down with the keyboard.

What is scroll view in Swift?

Overview. UIScrollView is the superclass of several UIKit classes, including UITableView and UITextView . A scroll view is a view with an origin that's adjustable over the content view. It clips the content to its frame, which generally (but not necessarily) coincides with that of the application's main window.

How to move the view when the keyboard height changes in SwiftUI?

SwiftUI will automatically update the view whenever the keyboard height changes. Add padding to the bottom of the view, which will make it move up and down with the keyboard. Update the view state with the latest value emitted by the keyboardHeight publisher. Now, when focusing and defocusing the text field, the view will move up and down:

How do I manage the iOS keyboard in SwiftUI?

It can be a TextField component from SwiftUI, a UITextField and UITextView from UIKit, or a text input field inside a web view. Whenever the iOS keyboard appears, it overlaps parts of your interface. The common approach to keyboard management is to move up the focused part of the view to avoid its overlapping.

What is newbooktableviewcontroller in Swift?

NewBookTableViewController.swift contains keyboard control logic, which monitors and detects user taps on the form fields and reacts accordingly. There are 7 UITextFields on the New Book view: Title, Author, Series, No in Series, Category (Genre), Source, and Format.

How do I make the object move with the keyboard?

The object will auto-move up with the keyboard, in sync. To select the bottom constraint you can also go to Size Inspector, then double-click on the constraint in the list - raywenderlich.com/wp-content/uploads/2015/09/… This worked perfect for me. it's literally a 2 step process. 1. Add the KeyboardLayoutConstraint.swift, 2.


2 Answers

Here is a solution, without handling the switch from one textField to another:

override func viewDidLoad() {         super.viewDidLoad()         NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)         NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)                 }     func keyboardWillShow(notification: NSNotification) {                 if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {         self.view.frame.origin.y -= keyboardSize.height     }             }  func keyboardWillHide(notification: NSNotification) {     self.view.frame.origin.y = 0 } 

To solve this, replace the two functions keyboardWillShow/Hide with these:

func keyboardWillShow(notification: NSNotification) {             if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() {         if view.frame.origin.y == 0 {             self.view.frame.origin.y -= keyboardSize.height         }     }         }  func keyboardWillHide(notification: NSNotification) {     if view.frame.origin.y != 0 {         self.view.frame.origin.y = 0     } } 

Swift 3.0:

override func viewDidLoad() {     super.viewDidLoad()                 NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)     NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)     }  @objc func keyboardWillShow(notification: NSNotification) {             if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {         if self.view.frame.origin.y == 0 {             self.view.frame.origin.y -= keyboardSize.height         }     }         }  @objc func keyboardWillHide(notification: NSNotification) {     if self.view.frame.origin.y != 0 {         self.view.frame.origin.y = 0     } }      

Swift 4.0:

override func viewDidLoad() {     super.viewDidLoad()                 NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)     NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)     }  @objc func keyboardWillShow(notification: NSNotification) {             if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {         if self.view.frame.origin.y == 0 {             self.view.frame.origin.y -= keyboardSize.height         }     }         }  @objc func keyboardWillHide(notification: NSNotification) {     if self.view.frame.origin.y != 0 {         self.view.frame.origin.y = 0     } } 

Swift 4.2:

override func viewDidLoad() {     super.viewDidLoad()                 NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)     NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil) }  @objc func keyboardWillShow(notification: NSNotification) {     if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {         if self.view.frame.origin.y == 0 {             self.view.frame.origin.y -= keyboardSize.height         }     } }  @objc func keyboardWillHide(notification: NSNotification) {     if self.view.frame.origin.y != 0 {         self.view.frame.origin.y = 0     } } 
like image 166
Boris Avatar answered Nov 07 '22 12:11

Boris


Easiest way that doesn't even require any code:

  1. Download KeyboardLayoutConstraint.swift and add (drag & drop) the file into your project, if you're not using the Spring animation framework already.
  2. In your storyboard, create a bottom constraint for the View or Textfield, select the constraint (double-click it) and in the Identity Inspector, change its class from NSLayoutConstraint to KeyboardLayoutConstraint.
  3. Done!

The object will auto-move up with the keyboard, in sync.

like image 40
gammachill Avatar answered Nov 07 '22 14:11

gammachill