你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS鍵盤擋住UITextView的解決方案

IOS鍵盤擋住UITextView的解決方案

編輯:IOS開發綜合
- (void)registerForKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)unregisterForKeyboardNotifications {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [self registerForKeyboardNotifications];
    _viewFrame = _inputDiaryView.frame;
}

- (void)viewDidDisappear:(BOOL)animated {
    [self unregisterForKeyboardNotifications];
}

- (void)keyboardWasShown:(NSNotification *)aNotification {
    CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView beginAnimations:TEXTVIEW_KEYBOARD context:nil];
    [UIView setAnimationDuration:animationDuration];
    _inputDiaryView.frame = CGRectMake(0, _viewFrame.origin.y - keyboardRect.size.height, _viewFrame.size.width, _viewFrame.size.height);
    [UIView commitAnimations];
}

- (void)keyboardWillBeHidden:(NSNotification *)aNotification{
    NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView beginAnimations:TEXTVIEW_KEYBOARD context:nil];
    [UIView setAnimationDuration:animationDuration];
    _inputDiaryView.frame = _viewFrame;
    [UIView commitAnimations];
}

_viewFrame:是UITextView的父控件的frame\

_inputDiaryView就是UITextView的父控件

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved