Created
July 18, 2012 22:05
-
-
Save nielsbot/3139219 to your computer and use it in GitHub Desktop.
How to overlay a transparent window on the system keyboard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface AppDelegate : UIResponder <UIApplicationDelegate> | |
@property (strong, nonatomic) IBOutlet UIWindow *window; | |
@property ( strong ) IBOutlet UITextField * textField ; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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