数字键盘没有关闭键盘的按钮,以下代码把”Done”拆成两个按钮了。
(后来觉得这个办法太麻烦了,不如在键盘上加一行工具栏,工具栏上加关闭按钮)
[[NSNotificationCenterdefaultCenter]addObserver:self
selector:@selector(keyboardWillShowOnDelay:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenterdefaultCenter]addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
- (void)keyboardWillShowOnDelay:(NSNotification *)notification
{
[self performSelector:@selector(keyboardWillShow:)withObject:nilafterDelay:0];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
UIView *foundKeyboard = nil;
UIWindow *keyboardWindow = nil;
for (UIWindow*testWindowin [[UIApplicationsharedApplication]windows])
{
if (![[testWindow class] isEqual:[UIWindowclass]])
{
keyboardWindow = testWindow;
break;
}
}
if (!keyboardWindow) return;
for (UIView*possibleKeyboardin [keyboardWindow subviews])
{
//NSLog(@"%@",[possibleKeyboard description]);
//iOS3
if ([[possibleKeyboard description] hasPrefix:@"<UIKeyboard"])
{
foundKeyboard = possibleKeyboard;
break;
}
else
{
// iOS4 sticks the UIKeyboard inside a UIPeripheralHostView.
if([[possibleKeyboarddescription]hasPrefix:@"<UIPeripheralHostView"]){
BOOL didFound =NO;
for (UIView*tmpViewin [possibleKeyboard subviews]) {
if ([[tmpViewdescription]hasPrefix:@"<UIKeyboard"]){
foundKeyboard= tmpView;
didFound= YES;
break;
}
}
if (didFound) {
break;
}
}
}
}
if (foundKeyboard)
{
UIButton *backButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
backButton.tag = 110;
backButton.frame = CGRectMake(215, 163, 52, 53);
backButton.adjustsImageWhenHighlighted=NO;
UIImage *darkImage = [[UIImageimageNamed:@"dark.png"]stretchableImageWithLeftCapWidth:5topCapHeight:0];
UIImage *darkCornerImage = [[UIImageimageNamed:@"darkCorner.png"]stretchableImageWithLeftCapWidth:5topCapHeight:0];
UIImage *brightImage = [[UIImageimageNamed:@"bright.png"]stretchableImageWithLeftCapWidth:5topCapHeight:0];
UIImage *brightCornerImage = [[UIImageimageNamed:@"brightCorner.png"]stretchableImageWithLeftCapWidth:5topCapHeight:0];
[backButton setBackgroundImage:darkImageforState:UIControlStateNormal];
[backButton setBackgroundImage:brightImageforState:UIControlStateHighlighted];
[backButton addTarget:selfaction:@selector(backButton:)forControlEvents:UIControlEventTouchUpInside];
[backButtonsetTitle:@"删除"forState:UIControlStateNormal];
[backButtonsetTitleColor:[UIColorblackColor]forState:UIControlStateHighlighted];
[foundKeyboard addSubview:backButton];
UIButton *doneButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
doneButton.tag = 111;
doneButton.frame = CGRectMake(215+52,163, 53, 53);
doneButton.adjustsImageWhenHighlighted=NO;
[doneButton setBackgroundImage:darkCornerImageforState:UIControlStateNormal];
[doneButton setBackgroundImage:brightCornerImageforState:UIControlStateHighlighted];
[doneButton addTarget:selfaction:@selector(doneButton:)forControlEvents:UIControlEventTouchUpInside];
[doneButton setTitle:@"隐藏"forState:UIControlStateNormal];
[doneButtonsetTitleColor:[UIColorblackColor]forState:UIControlStateHighlighted];
[foundKeyboardaddSubview:doneButton];
}
}
- (void)keyboardWillHide:(NSNotification *)notification
{
UIView *foundKeyboard = nil;
UIWindow *keyboardWindow = nil;
for (UIWindow*testWindowin [[UIApplicationsharedApplication]windows])
{
if (![[testWindow class] isEqual:[UIWindowclass]])
{
keyboardWindow = testWindow;
break;
}
}
if (!keyboardWindow) return;
for (UIView*possibleKeyboardin [keyboardWindow subviews])
{
//NSLog(@"%@",[possibleKeyboard description]);
//iOS3
if ([[possibleKeyboard description] hasPrefix:@"<UIKeyboard"])
{
foundKeyboard = possibleKeyboard;
break;
}
else
{
// iOS 4 sticks the UIKeyboard inside aUIPeripheralHostView.
if([[possibleKeyboarddescription]hasPrefix:@"<UIPeripheralHostView"]){
BOOL didFound =NO;
for (UIView*tmpViewin [possibleKeyboard subviews]) {
if ([[tmpViewdescription]hasPrefix:@"<UIKeyboard"]){
foundKeyboard= tmpView;
didFound= YES;
break;
}
}
if (didFound) {
break;
}
}
}
}
if (foundKeyboard)
{
UIView *back = [foundKeyboardviewWithTag:110];
[backremoveFromSuperview];
UIView *done = [foundKeyboardviewWithTag:111];
[doneremoveFromSuperview];
}
}