3

I have two UItextFields on my ViewController. The first one is in the top half of the ViewController but the second one is towards the bottom.

I have managed to get the keyboard to hide on return however, When I click into the second (lower down) UItextField the keyboard comes up and covers it. This means it is no longer possible to see what you are typing.

How do I move the ViewController up when the second UItextField is clicked so that the user can see what they are typing and then move the ViewController back down when the user presses return?

nhgrif
  • 58,130
  • 23
  • 123
  • 163
DannieCoderBoi
  • 728
  • 2
  • 10
  • 29
  • This might help (Objective-C): http://stackoverflow.com/questions/1247113/iphone-keyboard-covers-uitextfield – atwalsh Feb 06 '15 at 01:42
  • I'm new to iOS Development and have never touched Objective-C :/ Only started learning due to a basic swift course. – DannieCoderBoi Feb 06 '15 at 01:43
  • There are a number of stacks on this issue. Check out: [One of many][1] [1]: http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present?rq=1 – Syed Tariq Feb 06 '15 at 01:44
  • 1
    Here in swift. Check out @user3677173's answer: http://stackoverflow.com/questions/26070242/move-view-with-keyboard-using-swift – atwalsh Feb 06 '15 at 01:46
  • @atwalsh04 hat works great thanks. Would you know how to make this smoother as it is currently jumpy and also it ads the effect for both of the textfields. How could I do this for just one? – DannieCoderBoi Feb 06 '15 at 01:59
  • 1
    Better to design the screen in Static tableView. – Vineesh TP Feb 06 '15 at 06:10
  • @DannieCoderBoi I'm really not sure. All this does is shift the view up and down. – atwalsh Feb 06 '15 at 19:19

2 Answers2

0

use scrollView and scroll the textfield when keyboard will show

jamal zare
  • 563
  • 6
  • 8
0

Add scrollView in your Hole View (ViewController view) and then add notifications for hiding and showing keyboard

when keyboard will show change the scrollView Content insets by minusing keyboard hide: scrollView.ContentInsets.height - keyboard.height:

   func registerForKeyboardNotifications() {
    
    //Adding notifies on keyboard appearing
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIWindow.keyboardWillShowNotification, object: nil)
    
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIWindow.keyboardWillHideNotification, object: nil)
}

@objc
func keyboardWillShow(notification: NSNotification) {
    
    guard let keyboareRect =  notification.userInfo?[UIWindow.keyboardFrameBeginUserInfoKey] as? CGRect else { return }
    let keyboardSize = keyboareRect.size
    
    let insets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
    scrollView.contentInset = insets
    scrollView.scrollIndicatorInsets = insets
    
    var aRect = self.view.frame
    aRect.size.height -= keyboardSize.height
    
    guard let activeTextField = self.activeTextField else { return }
    
    if !aRect.contains(activeTextField.frame.origin) {
        scrollView.scrollRectToVisible(activeTextField.frame, animated: true)
    }
}

@objc
func keyboardWillHide(notification: NSNotification) {
    scrollView.contentInset = UIEdgeInsets.zero
    scrollView.scrollIndicatorInsets = UIEdgeInsets.zero
}
jamal zare
  • 563
  • 6
  • 8