Skip to content

Instantly share code, notes, and snippets.

@nielsbot
Created July 18, 2012 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nielsbot/3139219 to your computer and use it in GitHub Desktop.
Save nielsbot/3139219 to your computer and use it in GitHub Desktop.
How to overlay a transparent window on the system keyboard
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) IBOutlet UIWindow *window;
@property ( strong ) IBOutlet UITextField * textField ;
@end
@interface MyWindow : UIWindow
@end
@implementation MyWindow
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
return nil ;
}
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[ NSNotificationCenter defaultCenter ] addObserver:self selector:@selector( keyboardWillShow: ) name:UIKeyboardWillShowNotification object:nil ] ;
[[ NSNotificationCenter defaultCenter ] addObserver:self selector:@selector( keyboardDidHide: ) name:UIKeyboardDidHideNotification object:nil ] ;
UITapGestureRecognizer * g = [[ UITapGestureRecognizer alloc ] initWithTarget:self action:@selector( backgroundViewTapped ) ] ;
[ self.window.rootViewController.view addGestureRecognizer:g ] ;
return YES;
}
static MyWindow * __keyboardCoverWindow = nil ;
-(void)keyboardWillShow:(NSNotification*)note
{
CGRect keyboardFrame = [ [ note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey ] CGRectValue ] ;
if ( !__keyboardCoverWindow )
{
__keyboardCoverWindow = [[ MyWindow alloc ] initWithFrame:keyboardFrame ] ;
}
__keyboardCoverWindow.backgroundColor = [[ UIColor redColor ] colorWithAlphaComponent:0.5f ] ;
__keyboardCoverWindow.hidden = NO ;
__keyboardCoverWindow.windowLevel = 100.0f ;
}
-(void)keyboardDidHide:(NSNotification*)note
{
__keyboardCoverWindow.hidden = YES ;
}
-(void)backgroundViewTapped
{
[ self.textField resignFirstResponder ] ;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment