iOS軟鍵盤遮擋UITableView內文字框問題

躍然發表於2015-05-02

1、註冊
UIKeyboardDidShowNotification/UIKeyboardDidHideNotification通知。

-(id) initWithNibName:(NSString*)nibNameOrNil bundle:nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {

        // 寫在這裡,或者viewDidLoad
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShown:) name:UIKeyboardDidShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHidden:) name:UIKeyboardDidHideNotification object:nil];
    }
    return self;
}

-(void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}

2、當通知到來,調整frame。

-(void) keyboardShown:(NSNotification*) notification {
    _initialTVHeight = _tableView.frame.size.height;

    CGRect initialFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect convertedFrame = [self.view convertRect:initialFrame fromView:nil];
    CGRect tvFrame = _tableView.frame;
    tvFrame.size.height = convertedFrame.origin.y;
    _tableView.frame = tvFrame;
}

-(void) keyboardHidden:(NSNotification*) notification {
    CGRect tvFrame = _tableView.frame;
    tvFrame.size.height = _initialTVHeight;
    [UIView beginAnimations:@"TableViewDown" context:NULL];
    [UIView setAnimationDuration:0.3f];
    _tableView.frame = tvFrame;
    [UIView commitAnimations];
}

3、觸發文字框,滾動tableView

-(void) textFieldDidBeginEditing:(UITextField *)textField {
    NSIndexPath* path = [NSIndexPath indexPathForRow:row inSection:section];
    [self performSelector:@selector(scrollToCell:) withObject:path afterDelay:0.5f];
}
-(void) scrollToCell:(NSIndexPath*) path {
    [_tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionNone animated:YES];
}

4、蘋果給的解決方案是這樣的:
https://developer.apple.com/library/prerelease/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

demo 下載連結:
http://git.oschina.net/changyou/MyScrollViewKeyBoardDemo/repository/archive/master

5、或許這個更好

- (void)viewDidLoad {

//  監聽鍵盤的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];

 }

 #pragma mark - 鍵盤通知回撥方法
/*
 UIKeyboardAnimationCurveUserInfoKey = 7;   鍵盤執行動畫的節奏
 UIKeyboardAnimationDurationUserInfoKey = "0.25"; 鍵盤彈出動畫執行時間
 UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}"; 鍵盤尺寸
 UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}"; 鍵盤當前的位置
 UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}";  鍵盤改變後中心的位置
 UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}"; 鍵盤當前的frame
 UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}"; 鍵盤改變後的frame
 */
- (void)keyboardWillChangeFrameNotification:(NSNotification *) notification
{
//    NSLog(@"%@",notification.userInfo);
//  1.取出鍵盤當前的y座標
    CGRect beginFrame =  [notification.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    CGRect endFrame =  [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
//  2.中間的差值
    CGFloat minus =  endFrame.origin.y - beginFrame.origin.y ;
//  3.根據差值改變控制器view的frame
    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y += minus;
    self.view.frame = viewFrame;

}

/**
 *  拖拽tableView方法
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
//  退出鍵盤
    [self.view endEditing:YES];
}

#pragma mark -dealloc方法寫到最後
- (void)dealloc
{
//  移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

參考:

  1. http://blog.seancarpenter.net/2012/10/15/scrolling-a-uitableview-when-displaying-the-keyboard/

  2. Managing Text Fields and Text Views

相關文章