简体   繁体   中英

black area when moving keyboard

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
        }
    }
}

}

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM