iOS 系統數字鍵盤左下角加確定按鈕


首先在 viewWillAppear 方法中注冊監聽相應的鍵盤通知,並且要在 viewWillDisappear 方法中注銷通知
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; //注冊鍵盤顯示通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil]; //注冊鍵盤隱藏通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } -(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; //注銷鍵盤顯示通知 [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; }

處理鍵盤彈出和收起事件

// 鍵盤出現處理事件
- (void)handleKeyboardDidShow:(NSNotification *)notification
{
    // NSNotification中的 userInfo字典中包含鍵盤的位置和大小等信息
    NSDictionary *userInfo = [notification userInfo];
    // UIKeyboardAnimationDurationUserInfoKey 對應鍵盤彈出的動畫時間
    CGFloat animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    // UIKeyboardAnimationCurveUserInfoKey 對應鍵盤彈出的動畫類型
    NSInteger animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
    //數字彩,數字鍵盤添加“完成”按鈕
    if (doneInKeyboardButton){
        
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:animationDuration];//設置添加按鈕的動畫時間
        [UIView setAnimationCurve:(UIViewAnimationCurve)animationCurve];//設置添加按鈕的動畫類型
        
        //設置自定制按鈕的添加位置(這里為數字鍵盤添加“完成”按鈕)
        doneInKeyboardButton.transform=CGAffineTransformMakeTranslation(0, -53);
        
        [UIView commitAnimations];
    }
    
}

// 鍵盤消失處理事件
- (void)handleKeyboardWillHide:(NSNotification *)notification
{
    // NSNotification中的 userInfo字典中包含鍵盤的位置和大小等信息
    NSDictionary *userInfo = [notification userInfo];
    // UIKeyboardAnimationDurationUserInfoKey 對應鍵盤收起的動畫時間
    CGFloat animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    if (doneInKeyboardButton.superview)
    {
        [UIView animateWithDuration:animationDuration animations:^{
            //動畫內容,將自定制按鈕移回初始位置
            doneInKeyboardButton.transform=CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            //動畫結束后移除自定制的按鈕
            [doneInKeyboardButton removeFromSuperview];
        }];
        
    }
}

在textField代理中調用加載按鈕方法

#pragma mark -- UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    //初始化數字鍵盤的“完成”按鈕
    if(doneInKeyboardButton.superview==nil)
        [self configDoneInKeyBoardButton];
}
- (void) addDoneButton{
    //獲得鍵盤所在的window視圖
    NSArray *array= [[UIApplication sharedApplication]windows];
    
    for (UIWindow *window in array) {
        
        NSString *str=NSStringFromClass([window class]);
        
        if ([str isEqualToString:@"UIRemoteKeyboardWindow"]) {
            
            [window addSubview:doneInKeyboardButton];
            
        }
        
    }
    
}

//點擊“完成”按鈕事件,收起鍵盤
-(void)finishAction{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];//關閉鍵盤
}

如果用戶安裝了其他輸入法,會出錯,因此在APPDelegate中禁止第三方的輸入法

-(BOOL)application:(UIApplication *)application shouldAllowExtensionPointIdentifier:(NSString *)extensionPointIdentifier{
    
    return NO;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM