iOS自定义数字键盘

第一种方式:在获得键盘弹出通知时,在键盘的那个UIView上添加一个自定义的UIButto。

#define KEY_WIDTH 106

#define KEY_HEIGHT 53

#pragma mark – 处理TextField响应事件

– (void)editingDidBegin:(UITextField *)textF {

    [self.textFbecomeFirstResponder];

}

//3.实现通知处理

– (void)handleKeyboardWillHide:(NSNotification *)notification

{

    if (doneInKeyboardButton.superview)

    {

        [doneInKeyboardButtonremoveFromSuperview];

    }

}

– (void)handleKeyboardDidShow:(NSNotification *)notification

{

    NSDictionary *info = [notificationuserInfo];

    CGSize kbSize = [[infoobjectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;

    CGFloat normalKeyboardHeight = kbSize.height;

    int cnt = [[UIApplicationsharedApplication] windows].count;

    UIWindow* tempWindow = [[[UIApplicationsharedApplication] windows] objectAtIndex:cnt-1];

    // create custom button

    if (doneInKeyboardButton ==nil)

    {

        doneInKeyboardButton = [UIButtonbuttonWithType:UIButtonTypeCustom];        

        doneInKeyboardButton.frame =CGRectMake(18, tempWindow.frame.size.height53,106, 53);

        doneInKeyboardButton.adjustsImageWhenHighlighted =NO;

        [doneInKeyboardButtonsetImage:[UIImageimageNamed:@”done.png”]forState:UIControlStateNormal];

        [doneInKeyboardButtonsetImage:[UIImageimageNamed:@”done.png”]forState:UIControlStateHighlighted];

        [doneInKeyboardButtonaddTarget:selfaction:@selector(finishAction)forControlEvents:UIControlEventTouchUpInside];

    }

    // locate keyboard view

    if (doneInKeyboardButton.superview == nil)

    {

        // 注意这里直接加到window

        [tempWindow addSubview:doneInKeyboardButton];

    }

}  

#pragma mark – 处理视图响应事件

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [self.textFresignFirstResponder];

}

-(void)viewWillAppear:(BOOL)animated

{

    [superviewWillAppear:animated];

   //1. 先注册通知    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(handleKeyboardDidShow:)name:UIKeyboardDidShowNotificationobject:nil];

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(handleKeyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];

}

//2. dealloc中反注册通知

-(void)dealloc

{

    [[NSNotificationCenterdefaultCenter] removeObserver:self];

}

第二种方法:自己写一个键盘。😆

分享到: 更多