IOS 關於鍵盤遮擋編輯地區(UITextFiled/UITextView)的問題

來源:互聯網
上載者:User

標籤:

  參考自:http://blog.csdn.net/windkisshao/article/details/21398521

1.自定方法 ,用於行動裝置檢視

-(void)moveInputBarWithKeyboardHeight:(float)_CGRectHeight withDuration:(NSTimeInterval)_NSTimeInterval;

2.註冊監聽

NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];

    [defaultCenter selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    [defaultCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

3.實現方法

- (void)keyboardWillShow:(NSNotification *)notification {

    NSDictionary *userInfo = [notification userInfo];

    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValue CGRectValue];

    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSTimeInterval animationDuration;

    [animationDurationValue getValue:&animationDuration];

    if(nil==self.myTextView) return;//    self.editTextView 為被鍵盤遮擋住的控制項

    CGRect rect = self.myTextView.frame;

    float textY = rect.origin.y + rect.size.height; 

    float bottomY = SCREENHEIGHT - textY;//得到下邊框到底部的距離  SCREENHEIGHT 為當前裝置的高度

    if(bottomY >=keyboardRect.size.height ){//鍵盤預設高度,如果大於此高度,則直接返回

        return;

    }

    float moveY = keyboardRect.size.height - bottomY;

    [self moveInputBarWithKeyboardHeight:moveY withDuration:animationDuration];

}

 

- (void)keyboardWillHide:(NSNotification *)notification {

    NSDictionary* userInfo = [notification userInfo];

    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSTimeInterval animationDuration;

    [animationDurationValue getValue:&animationDuration];

    [self moveInputBarWithKeyboardHeight:0.0 withDuration:animationDuration];

}

 

-(void)moveInputBarWithKeyboardHeight:(float)_CGRectHeight withDuration:(NSTimeInterval)_NSTimeInterval{

  CGRect rect1 = self.view.frame;

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:_NSTimeInterval];

    rect1.origin.y = -_CGRectHeight;//view往上移動

    self.view.frame = rect1;

    [UIView commitAnimations];

}

測試的時候是用的4s真機測試的,一切都很完美,但是前幾天同事用6 測的時候發現一個問題,就是編輯的時候能達到不遮擋控制項的效果,但是發現輸入中文的時候有時候會莫名的敲不出中文,會出現一連串的字元,而且刪除的時候字元刪不掉,幾乎是越刪越多.但是用4S測了很多遍都沒有出現這個問題.這幾天一直在想這個問題是什麼造成的,望知道的高人們,指點一二.感謝!(用的UITextView)

IOS 關於鍵盤遮擋編輯地區(UITextFiled/UITextView)的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.