動態擷取鍵盤高度

來源:互聯網
上載者:User

標籤:os   使用   io   for   ar   cti   log   size   sp   

//在遇到有輸入的情況下。由於現在鍵盤的高度是動態變化的。中文輸入與英文輸入時高度不同。所以輸入框的位置也要做出相應的變化#pragma mark - keyboardHight-(void)viewWillAppear:(BOOL)animated{    [self registerForKeyboardNotifications];}-(void)viewWillDisappear:(BOOL)animated{    [[NSNotificationCenter defaultCenter] removeObserver:self];}- (void)registerForKeyboardNotifications{    //使用NSNotificationCenter 鍵盤出現時    [[NSNotificationCenter defaultCenter] addObserver:self                                                  selector:@selector(keyboardWasShown:)                                                      name:UIKeyboardDidShowNotification object:nil];        //使用NSNotificationCenter 鍵盤隱藏時    [[NSNotificationCenter defaultCenter] addObserver:self                                                  selector:@selector(keyboardWillBeHidden:)                                                      name:UIKeyboardWillHideNotification object:nil];         }//實現當鍵盤出現的時候計算鍵盤的高度大小。用於輸入框顯示位置- (void)keyboardWasShown:(NSNotification*)aNotification{    NSDictionary* info = [aNotification userInfo];    //kbSize即為鍵盤尺寸 (有width, height)    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//得到鍵盤的高度    NSLog(@"hight_hitht:%f",kbSize.height);    if(kbSize.height == 216)    {        keyboardhight = 0;    }    else     {        keyboardhight = 36;   //252 - 216 系統鍵盤的兩個不同高度    }    //輸入框位置動畫載入    [self begainMoveUpAnimation:keyboardhight];}//當鍵盤隱藏的時候- (void)keyboardWillBeHidden:(NSNotification*)aNotification{     //do something}//(TextView) 當鍵盤開始輸入前。時行計算與動畫載入-(void)textViewDidBeginEditing:(UITextView *)textView{    NSLog(@"gegin animation");    sendMsgTextView =textView;    resultCommunityTableview.frame = CGRectMake(0, 36, 320, 150);    //動畫載入    [self begainMoveUpAnimation:0.0 ];    }//關閉鍵盤(TextView) 換行時。隱藏鍵盤-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text  {      resultCommunityTableview.frame = CGRectMake(0, 36, 320, 376);    if ([text isEqualToString:@"\n"]) {          [textView resignFirstResponder];          return NO;      }    return YES;  }  //輸入結束時調用動畫(把按鍵。背景。輸入框都移下去)-(void)textViewDidEndEditing:(UITextView *)textView{    NSLog(@"tabtabtab");    [self endEditAnimation];        //釋放    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];}//判斷當前IME-(void)textViewDidChangeSelection:(UITextView *)textView{    NSLog(@"wewe:%@",[[UITextInputMode currentInputMode] primaryLanguage]);    /*    if ([[UITextInputMode currentInputMode] primaryLanguage] == @"en-US") {         NSLog(@"en-US");     }     else     {         NSLog(@"zh-hans");     }      */}

動態擷取鍵盤高度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.