Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Keyboard frame without keyboardWillAppear userinfo notification, iOS

I am developing an iPhone app in which I have to show some customize picker in case of a button event. But I don't want to hard coded values for the frame of my custom pickerView. I searched and found keyboardWillShow notification userInfo method, but in my case I am not showing keyboard so can't get frame out of it. Can anybody help me way out for getting keyboard frame that I can use for my customize picker?

like image 415
Himanshu Avatar asked Oct 19 '22 04:10

Himanshu


1 Answers

You can use a UITextField and call becomeFirstResponder and immediately resignFirstResponder on it, so it will be shown and hidden without actually seeing the keyboard.

the action method of the button will look like the following:

@IBAction func showPickerView(sender: AnyObject)
{
    let textField = UITextField()
    view.addSubview(textField)
    textField.becomeFirstResponder()
    textField.resignFirstResponder()
    textField.removeFromSuperview()
}

And you could listen to the notification and get the height.

for further information: Get height of iOS keyboard without UIKeyboardWillShowNotification

like image 135
Erfan Avatar answered Oct 21 '22 06:10

Erfan