判斷虛擬鍵盤是否打開的代碼


//第一種直接在textview的狀態里判斷 記得寫textview的委托
- (void)textViewDidBeginEditing:(UITextView *)textView { bKeyBoardHide = NO; } - (void)textViewDidEndEditing:(UITextView *)textView { bKeyBoardHide = YES; }

 

第二種 鍵盤監控

[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotificationobject:nil];
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotificationobject:nil];
 -(void)keyboardWillHide:(NSNotification *)notification
{
     NSLog(@"*-----HideKeyBoard");
     bKeyBoardHide = YES;
}

-(void)keyboardWillShow:(NSNotification *)notification
{
    NSLog(@"*-----ShowKeyBoard");
    bKeyBoardHide = NO;
}

/*
@implementation UIView (FindAndResignFirstResponder)

- (BOOL)findAndResignFirstResponder 

{
    if (self.isFirstResponder) 

    {
        [self resignFirstResponder];
       return YES;     
    }

   for (UIView* subView in self.subviews) 

    {
        if ([subView findAndResignFirstResponder])
       return YES;
    }
    return NO;
}

@end


//自動尋找並釋放第一焦點
- (BOOL) TTIsKeyboardVisible

{
    // Operates on the assumption that the keyboard is visible if and only if there is a first
    // responder; i.e. a control responding to key events
    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    return [window findAndResignFirstResponder];
}

*/

 


免責聲明!

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



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