Get Keyboard frame without keyboardWillAppear userinfo notification, iOS - 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?

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

Related

Custom keyboard on UITextView shows apple keyboard first then switches to the custom keyboard

Short video of what happens when tapping on a textView:
https://www.youtube.com/watch?v=LG_r3iDJKiM
I'm trying to show my custom keyboard on a UITextView from the textViewDidBeginEditing function, The custom keyboard has been tested on UITextFields and is working as intended. Now I'm trying to get the same keyboard to work with UITextViews.
When opening the app for the first time, and tapping on a textView the first time will have this behavior, then when we tap on other textViews the custom keyboard is there directly as intended.
the Current code in my TextViewDidBeginEditing, I tried changing the order of the last 4 lines but with no success:
public func textViewDidBeginEditing(_ textView: UITextView) {
keyboard = Keyboard.sharedInstance.keyboardWithType(availableKeyboards.numbers.rawValue, returnKey:textView.returnKeyType, isSecure: false,decimalSeparator:self.decimalSeparatorCharacter, responderClass: self)
inputView.keyboardAppearance = .dark
inputView.hideAssistantBar()
inputView.inputView = keyboard
inputView.reloadInputViews()
}
This will result in the video above, I tried some other ways but no success yet:
Setting a empty UIView as the inputView, this will still result in the apple keyboard shortly appearing
let view:UIView = UIView()
textView.inputView = view
also tried doing reloadInputViews but that doesn't work either:
textView.reloadInputViews()
also tried running it all on the main tread with
DispatchQueue.main.async { }
but no success, I cannot understand why even setting a UIView() as the inputView will still shortly show the apple keyboard. Any help in the right direction would be greatly appreciated!
Thanks in advance!

While displaying an alert in IOS, keyboard does not resign from View

While displaying an alert(Wrong password) in IOS 8, keyboard opens automatically and hide the alert(just in Iphone 4s because of the screen's size), so I can't click in "OK" and I also can't dismiss keyboard because first I need to close the alert!
Keyboard hides alert
(It seems the app is recovering last keyboard's state and showing up again)
How can I close the keyboard before calling the alert?(this way the state will be "closed")
I've tried:
myTextField!.resignFirstResponder()
While calling the button, but it didn't work, the alert shows up and automatically the keyboard opens over it !
if myTextField!.resignFirstResponder() is not working properly try this when you present the alert before call this -->self.view.endEditing(true)
the above function is not work well , try
Choice -1 :Using the Responder Chain
UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil)
This will resign the first responder (and dismiss the keyboard) every time, without you needing to send resignFirstResponder to the proper view. No matter what, this will dismiss the keyboard. It’s by far the best way to do it: no worrying about who the first responder is
Choice -2 :UIView’s endEditing
(assuming your text field is a subview of the view you call this on). Most of the time:
self.view.endEditing(true)
set Delegate to myTextField
Include
func textFieldShouldReturn(textField: UITextField) -> Bool
{
textField .resignFirstResponder()
return true
}
Other wise Try the following
var activeField : UITextField!
func textFieldDidBeginEditing(textField: UITextField)
{
activeField = textField
}
func textFieldDidEndEditing(textField: UITextField)
{
activeField = nil
}
func textFieldShouldReturn(textField: UITextField) -> Bool
{
textField .resignFirstResponder()
return true
}
Call activeField.resignFirstResponder() before alert appears
I think from iOS8 you need to use UIAlertController instead of UIAlertView. Using UiAlertView in iOS8 and above is causing keyboard to popup unnecessarily. I have seen this and i made a condition to use UIAlertController for iOS8 and above. In below version UIAlertView should work fine
This is UIAlertView bug on iOS 8.
I have same problem but UIAlertController has not problem. :3
UIAlertView was deprecated since iOS8.
https://developer.apple.com/reference/uikit/uialertview
In Swift 4 : below code worked for me
DispatchQueue.main.async() {
self.view.endEditing(true)
}

UITextView doesn't switch input toolbar to custom keyboard

iOS9 Xcode7 beta6: I'm trying to switch between keyboards (custom/default) for UITextView by using reloadInputViews(). Changing UIKeyboardType and UIKeyboardAppearance by calling reloadInputViews() works perfectly. Also following code works well under iOS8.
This implies that textView is already a first responder:
private func showCustomKeyboard() {
textView.inputView = customKeyboardView
textView.reloadInputViews()
}
private func showDefaultKeyboard() {
textView.inputView = nil
textView.reloadInputViews()
}
Things like the following have made no effect and also they look like overkill:
textView.inputView.resignFirstResponder()
textView.inputView.becomeFirstResponder()
textView.inputView = customKeyboardView
textView.reloadInputViews()
I found a couple of related questions on SO but no one of them doesn't have to do with iOS9 and as I said before it does work in iOS8.
Have anyone come across this bug?
Did you try to change the order? Because you dismiss and after that show a keyboard again. Does it make sense?:
textView?.inputView.resignFirstResponder() // dismiss keyboard
textView?.inputView.becomeFirstResponder() // show keyboard
textView?.inputView = customKeyboardView // reassign new keyboard
textView?.reloadInputViews() // reload keyboard
Try:
textView?.inputView.resignFirstResponder() // dismiss keyboard
textView?.inputView = customKeyboardView // reassign new keyboard
textView?.reloadInputViews() // reload keyboard
textView?.inputView.becomeFirstResponder() // show keyboard
The bug was related to a simulator with iOS9 on the board and eventually has been fixed with unchecking Keyboard -> Connect -> Hardware Keyboard.

iOS - check if keyboard blocks view

How can I check if the keyboard is blocking my view?
I have UIButton that is in the centre of the screen. I also have a UITextfield that makes the keyboard appears. When I run the app on iPhone 4 the keyboard block the button, but on other models, it doesn't. I have a method that scrolls up the view when the keyboard appears. But I only want to scroll up in case the view is blocked. I can check the model of the iPhone and then decide if to scroll or not, but I thought checking if the Button is blocked would be better. How can I do it?
I solved this problem in an app like so:
Embed the UI for that screen in a UIScrollView, but configure it so that scrolling is not enabled (scrollEnabled property from code, checkbox if you're using a storyboard).
When the keyboard notification is received, get the frame from the button, then call scrollRectToVisible:animated: on the scroll view. It'll move the content the minimum amount necessary to make the button visible, which will be not at all if the screen is big enough.
Using the UIKeyboardWillShowNotification, you can get the height of the keyboard like this:
NSValue *keyboardRect = [notification.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGFloat keyboardHeight = MIN(keyboardRect.CGRectValue.size.width, keyboardRect.CGRectValue.size.height);
The get the relevant "lowest" point of your button. (like buttonMaxY = CGRectGetMaxY(yourButton.frame)).
Use the scroll methods you have implemented, but scroll only if necessary: keyboardHeight+buttonMaxY > the height of the screen.
When keyboard is about to appear, a UIKeyboardWillShowNotification notification is posted with the frame of the keyboard. You can calculate and see if the text field frame intersects with the keyboard frame, and scroll.
See documentation on keyboard notifications here.
Following Leo Natan's explanation, you can try something like:
func keyboardWillShow(notification: Notification) {
guard let keyboardFrame = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? CGRect,
let viewFrame = myView.frame else {
log.error("I cannot calculate keyboard & view frame")
return
}
if keyboardFrame.intersects(viewFrame) {
// view covered by Keyboard
}
}
pay attention to use UIKeyboardFrameEndUserInfoKey, you can check this link for further details.

Notification for change of iPad keyboard height

I am trying to show a UITextField on top of the iPad keyboard.
I was able to get the height of the keyboard when it was presented with the notification.
However, in iPad, by change the language input of the keyboard -> most likely to Japanese, the height of the keyboard changed because a text-hypothesis area was shown on top of the keyboard, that caused my UITextfield hidden by that area....
Does anybody know how can I get the height changed notification or any other way?
The answer is that when you switch languages, the UIKeyboardDidShowNotification fires for each change, so you always get the updated height.
See my answer here on how to set up responses to the showing and hiding, and getting the height.
Swift
The UIKeyboardDidShowNotification won't fire anymore the keyboard size change.
Use UIKeyboardWillChangeFrameNotification instead:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(instance.keyboardWillChange(_:)), name:UIKeyboardWillChangeFrameNotification, object: nil)
at the function:
let targetSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
Important: this event will be fired also when keyboard will open and will hide, can replace both UIKeyboardWillShowNotification and UIKeyboardWillHideNotification if only sizes are needed

Resources