// // IMGViewController.h // HackKeyboard // // Created by Mamong on 13-8-26. // Copyright (c) 2013年 Mamong. All rights reserved. // #import <UIKit/UIKit.h> @interface IMGViewController : UIViewController<UITextFieldDelegate> @property (strong, nonatomic) UITextField *nameTF; @property (strong, nonatomic) UITextField *ageTF; @property (strong, nonatomic) UITextField *countryTF; @end
// // IMGViewController.m // HackKeyboard // // Created by Mamong on 13-8-26. // Copyright (c) 2013年 Mamong. All rights reserved. // #import "IMGViewController.h" @interface IMGViewController () @property (strong, nonatomic) UIView *keyboardView; @end @implementation IMGViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UILabel *labelName = [[UILabel alloc]initWithFrame:CGRectMake(20.0, 50.0, 60.0, 30.0)]; labelName.text = @"Name:"; [self.view addSubview:labelName]; UILabel *labelAge = [[UILabel alloc]initWithFrame:CGRectMake(20.0, 100.0, 40.0, 30.0)]; labelAge.text = @"Age:"; [self.view addSubview:labelAge]; UILabel *labelCountry = [[UILabel alloc]initWithFrame:CGRectMake(20.0, 150.0, 80.0, 30.0)]; labelCountry.text = @"Country:"; [self.view addSubview:labelCountry]; _nameTF = [[UITextField alloc]initWithFrame:CGRectMake(120.0, 50.0, 100.0, 30.0)]; _nameTF.tag = 100; _nameTF.borderStyle = UITextBorderStyleRoundedRect; _nameTF.keyboardType = UIKeyboardTypeAlphabet; _nameTF.delegate = self; [self.view addSubview:_nameTF]; _ageTF = [[UITextField alloc]initWithFrame:CGRectMake(120.0, 100.0, 100.0, 30.0)]; _ageTF.tag = 101; _ageTF.borderStyle = UITextBorderStyleLine; _ageTF.keyboardType = UIKeyboardTypeNumberPad; _ageTF.delegate = self; [self.view addSubview:_ageTF]; _countryTF = [[UITextField alloc]initWithFrame:CGRectMake(120.0, 150.0, 100.0, 30.0)]; _countryTF.tag = 102; _countryTF.borderStyle = UITextBorderStyleBezel; _countryTF.keyboardType = UIKeyboardTypeAlphabet; _countryTF.returnKeyType = UIReturnKeyDone; _countryTF.delegate = self; [self.view addSubview:_countryTF]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidDisappear:) name:UIKeyboardDidHideNotification object:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)keyboardDidShow:(NSNotification *)notification { [self hackViewForKeyboard]; } - (void)keyboardDidDisappear:(NSNotification *)notification { [[_keyboardView viewWithTag:1000] removeFromSuperview]; [[_keyboardView viewWithTag:1001] removeFromSuperview]; } - (void)hackViewForKeyboard { _keyboardView = [self findKeyboardView]; UIButton *hackButton; if(_keyboardView &&[_nameTF isFirstResponder]) { [[_keyboardView viewWithTag:1001] removeFromSuperview]; hackButton = [UIButton buttonWithType:UIButtonTypeCustom]; hackButton.tag = 1000; hackButton.frame = CGRectMake(3.0, 174.0, 75.0, 41.0); hackButton.backgroundColor = [UIColor darkGrayColor]; [hackButton setTitle:@".?123" forState:UIControlStateNormal]; [hackButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; [_keyboardView addSubview:hackButton]; } if (_keyboardView &&[_ageTF isFirstResponder]) { [[_keyboardView viewWithTag:1000] removeFromSuperview]; hackButton = [UIButton buttonWithType:UIButtonTypeCustom]; hackButton.tag = 1001; hackButton.frame = CGRectMake(0.0, 163.0, 106.0, 53.0); [hackButton setTitle:@"Return" forState:UIControlStateNormal]; [hackButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; [hackButton addTarget:self action:@selector(returnButtonDone:) forControlEvents:UIControlEventTouchUpInside]; [_keyboardView addSubview:hackButton]; } } - (void)returnButtonDone:(id)sender { [_countryTF becomeFirstResponder]; } - (UIView*)findKeyboardView { NSArray* windowList = [[UIApplication sharedApplication] windows]; for(UIWindow* theWindow in [windowList reverseObjectEnumerator]) { for(UIView* theView in [theWindow subviews]) { if(!strcmp(object_getClassName(theView), "UIPeripheralHostView") || !strcmp(object_getClassName(theView), "UIKeyboard")) { return theView; } } } return nil; } #pragma mark #pragma UITextFieldDelegate Methods -(BOOL)textFieldShouldReturn:(UITextField *)textField { switch (textField.tag) { case 100: [_ageTF becomeFirstResponder]; break; case 102: [_countryTF resignFirstResponder]; break; default: break; } return YES; } - (void)textFieldDidBeginEditing:(UITextField *)textField { [[NSNotificationCenter defaultCenter]postNotificationName:UIKeyboardDidShowNotification object:nil]; } - (void)textFieldDidEndEditing:(UITextField *)textField { [[NSNotificationCenter defaultCenter]postNotificationName:UIKeyboardDidHideNotification object:nil]; } @end
效果图: