Why doesn't the keyboard show when my text input view is tapped?
Q: Why doesn’t the keyboard show when my text input view is tapped?
A: There are several reasons why the keyboard doesn't show:
First, it may be because the keyboard is covered by a higher-level window (see
windowLevel for more information). In iOS, the keyboard is a subview of an independent window. The window has a higher level than the application's key window, so it is always displayed in the front. If you create another window in the overlapped position and make its level higher than that of the keyboard window, then the keyboard would be covered, thus becoming invisible to your users. You can confirm this by checking the relevant attributes of your application windows, as shown in Listing 1:Listing 1 Printing out the relevant attributes of windows
for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
NSLog(@"isKeyWindow = %d window level = %.1f frame = %@ class = %@\n",
window.isKeyWindow, window.windowLevel,
NSStringFromCGRect(window.frame), window.class.description);
}
Listing 2 provides an example log when the keyboard is covered by a higher-level window (
MyCustomWindow
) in the overlapped position.Listing 2 An example log when the keyboard is covered by a higher-level window
TheApp[22892:60b] isKeyWindow = 1 window level = 0.0 frame = {{0, 0}, {320, 480}}
class = UIWindow
TheApp[22892:60b] isKeyWindow = 0 window level = 1.0 frame = {{0, 0}, {320, 480}}
class = UITextEffectsWindow
TheApp[22892:60b] isKeyWindow = 0 window level = 2000.0 frame = {{0, 200}, {320, 280}}
class = MyCustomWindow
The solution is to avoid using multiple windows by replacing the extra window with a view or view hierarchy. In iOS, an application should only have one window unless it needs to display content on an external device screen (in this case, the extra window is normally displayed on a new screen). This ensures the keyboard, when shown, is always in the front of the screen.
Secondly, your keyboard may not show because your text input view failed to become the first responder. This may be due to any of the following reasons:
Your text input view is not allowed to become the first responder. The implementation of
-
(BOOL)canBecomeFirstResponder
method inUIView
returnsNO
by default; thus, preventing view instances from becoming the first responder. Therefore, if your view class is directly derived fromUIView
, then you need to override the above method to returnYES
as shown in Listing 3. Doing so will ensure that your text input view is allowed to become the first responder.Listing 3 Allowing a custom view to become the first responder
- (BOOL)canBecomeFirstResponder {
return YES;
}
Your text input view refuses to become the first responder. This may happen when the
-
(BOOL)becomeFirstResponder
method of your view class returnsNO
. If you override this method for some reasons, be sure that it returnsYES
when the text input view is interactive.
Lastly, the issue may be related to the auto-rotation support of view controllers. Since iOS 6,
UIViewController
's subclasses support auto-rotation by returning an orientation bit mask in the- (NSUInteger)supportedInterfaceOrientations method.UIKit
uses the bit mask to calculate the keyboard frame for current user interface orientation. Returning a wrong bit mask may cause the keyboard to be placed outside the screen. So if your application supports auto-rotation, be sure that the-
(NSUInteger)supportedInterfaceOrientations
method is implemented and returns the right bit mask. SeeUIInterfaceOrientationMask for valid bit-mask values.
Document Revision History
Date | Notes |
---|---|
2014-05-14 | New document that explains why the keyboard doesn't show when a text input view is tapped. |
Copyright © 2014 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2014-05-14