0

I have a textfield on the lower half of the view so I'm using functions below to move my view. But actually everything looks as I want to be except a black area atop of keyboard.How to deal with it?Any help is appreciated. here's the screen

override func viewDidLoad() {

        NotificationCenter.default.addObserver(self, selector: #selector(ViewControllerForTextfield.keyboardWillShow(_:)), name:NSNotification.Name.UIKeyboardWillShow, object: nil);
        NotificationCenter.default.addObserver(self, selector: #selector(ViewControllerForTextfield.keyboardWillHide(_:)), name:NSNotification.Name.UIKeyboardWillHide, object: nil);
        textField.delegate = self
        super.viewDidLoad()
    }


func keyboardWillShow(_ sender: Notification) {
    if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y == 0{
            self.view.frame.origin.y -= keyboardSize.height + 100
        }
    }

}

func keyboardWillHide(_ sender: Notification) {
    if let keyboardSize = (sender.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y != 0{
            self.view.frame.origin.y += keyboardSize.height
        }
    }
}

}

tyg1
  • 47
  • 2
  • 6

1 Answers1

0

I believe 310 is too much to move up. As of custom keyboards, it is not easy to determine the height of the keyboard. However, this post explains how to get the height of the keyboard.

EDIT: Maybe you also want to substitute the difference of the distance from the textfield to the bottom of the screen?

Community
  • 1
  • 1
Yannick Winter
  • 103
  • 2
  • 11
  • I agree 310 that's too much but I have an ImageView atop of the textfield so i don't want to crop it .Thank you for the answer though – tyg1 Nov 12 '16 at 11:57
  • @tyg1 I edited my answer. Maybe it the keyboard + TextField + ImageView doesn't fit to the screen. If this is the case you will either need to change the size of you ImageView or to crop it. However please mark the answer as correct if you feel it is. – Yannick Winter Nov 12 '16 at 13:23