ios8数字键盘添加完成

来源:互联网 发布:大陆台湾网络骂战 编辑:程序博客网 时间:2024/03/28 19:51

 [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(addDoneButtonToNumPadKeyboard)name:UIKeyboardDidShowNotificationobject:nil];

    //注册键盘隐藏通知

    [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(removeDoneButtonFromNumPadKeyboard)name:UIKeyboardDidHideNotificationobject:nil];


- (void)addDoneButtonToNumPadKeyboard

{

    

   

    

    UIButton *doneButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

    if ([[[UIDevicecurrentDevice] systemVersion]floatValue] < 8.0){

        doneButton.frame =CGRectMake(0,163, 106, 53);

    }else{

        doneButton.frame =CGRectMake(0self.view.frame.size.height-53,106, 53);

    }

    doneButton.tag =20;

    doneButton.adjustsImageWhenHighlighted =NO;

    [doneButton setTitle:@"完成"forState:UIControlStateNormal];

    [doneButton setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

    [doneButton addTarget:selfaction:@selector(doneButton:)forControlEvents:UIControlEventTouchUpInside];

    

   NSArray *windowArr = [[UIApplicationsharedApplication] windows];

   if (windowArr != nil && windowArr.count >1){

       UIWindow *needWindow = [windowArr objectAtIndex:1];

       UIView *keyboard;

       for(int i =0; i < [needWindow.subviewscount]; i++) {

            keyboard = [needWindow.subviewsobjectAtIndex:i];

           NSLog(@"%@", [keyboarddescription]);

           if(([[keyboard description]hasPrefix:@"<UIPeripheralHostView"] ==YES) || ([[keyboard description] hasPrefix:@"<UIKeyboard"] ==YES) || ([[keyboard description] hasPrefix:@"<UIInputSetContainerView"] ==YES)){

                

               UIView *doneButtonView = [keyboard viewWithTag:20];

               if (doneButtonView == nil){

                    [keyboardaddSubview:doneButton];

                }

            }

        }

    }

}


-(void)removeDoneButtonFromNumPadKeyboard

{

   UIView *doneButton = nil;

    

   NSArray *windowArr = [[UIApplicationsharedApplication] windows];

   if (windowArr != nil && windowArr.count >1){

       UIWindow *needWindow = [windowArr objectAtIndex:1];

       UIView *keyboard;

       for(int i =0; i < [needWindow.subviewscount]; i++) {

            keyboard = [needWindow.subviewsobjectAtIndex:i];

           if(([[keyboard description]hasPrefix:@"<UIPeripheralHostView"] ==YES) || ([[keyboard description] hasPrefix:@"<UIKeyboard"] ==YES) || ([[keyboard description] hasPrefix:@"<UIInputSetContainerView"] ==YES)){

                doneButton = [keyboardviewWithTag:20];

               if (doneButton != nil){

                    [doneButtonremoveFromSuperview];

                }

            }

        }

    }

}



0 0
原创粉丝点击