给keyboard增加删除按钮

数字键盘没有关闭键盘的按钮,以下代码把”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];

     }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值