IOS keyboard layout and orientation

I am relatively new to the iOS SDK, and I am having a very strange problem regarding the location and orientation of the device keyboard for the application I'm working on. The problem is that if the keyboard is open when user multitasking applications or the application goes into the background, after the user returns to the application, the keyboard will be biased (when lifting UIKeyboardWillChangeFrameNotification ), but in the wrong orientation and location.

Sometimes the keyboard is also fully displayed on the screen, which is completely undesirable behavior.

My questions:

  • What is the position and orientation of the keyboard depending on? How is it controlled by iOS?

  • Is there a way to detect when the keyboard is displayed overs regardless of device type and screen size? I think this is doable by tracking the UIKeyboardWillChangeFrameNotification or the UIKeyboardWillShowNotification .

  • How could I reset / set the location and orientation of the keyboard before displaying it? Is it possible?

+3
source share
2 answers

1.) The keyboard is UIWindow, the position depends on the main application window.

2.) What you can do is that at one of the starts of the notification method, UIKeyboardWillShowNotification or UIKeyboardWillChangeFrameNotification scan the preview windows to find the Keyboard. In one of my applications, I needed to add a subview to the keyboard. For your case, you can get the frame by doing the following:

 //The UIWindow that contains the keyboard view - It some situations it will be better to actually //iterate through each window to figure out where the keyboard is, but In my applications case //I know that the second window has the keyboard so I just reference it directly UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; //Because we cant get access to the UIPeripheral throught the SDK we will just use UIView. //UIPeripheral is a subclass of UIView anyways UIView* keyboard; //Iterate though each view inside of the selected Window for(int i = 0; i < [tempWindow.subviews count]; i++) { //Get a reference of the current view keyboard = [tempWindow.subviews objectAtIndex:i]; //Assuming this is for 4.0+, In 3.0 you would use "<UIKeyboard" if([[keyboard description] hasPrefix:@"<UIPeripheral"] == YES) { //Keyboard is now a UIView reference to the UIPeripheral we want NSLog(@"Keyboard Frame: %@",NSStringFromCGRect(keyboard.frame)); } } 

3.) Not quite sure that this is possible, but with the code I provided. keyboard now passed to 'UIView', to which you can apply your own transformations.

It may not be the most elegant solution, but it works well for my case.

Hope this helps!

+1
source

From the documentation:

Use the keys described in the Keyboard User Information Keys to get the location and size of the keyboard from the userInfo dictionary.

Keys used to get values ​​from the user information dictionary of keyboard notifications:

 NSString * const UIKeyboardFrameBeginUserInfoKey; NSString * const UIKeyboardFrameEndUserInfoKey; NSString * const UIKeyboardAnimationDurationUserInfoKey; NSString * const UIKeyboardAnimationCurveUserInfoKey; 
+5
source

Source: https://habr.com/ru/post/956091/


All Articles