隐藏键盘后,滚动 View 应回到其原始contentInset,但在iOS7中不起作用。当显示键盘时,设置scrollview的contentInset可以工作,但是在隐藏键盘时,scrollview的contentInset不能设置为inset零。
编码:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:Nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil];
}

- (void)keyboardWasShown:(NSNotification *)notif
{
    CGSize keyboardSize = [[[notif userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0);


    UIScrollView *scrollView = (UIScrollView *)self.view;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

    CGRect rect = self.view.frame;
    rect.size.height -= keyboardSize.height;
    if (!CGRectContainsPoint(rect, self.wishContentField.frame.origin)) {
        CGPoint point = CGPointMake(0, self.wishContentField.frame.origin.y - keyboardSize.height);
        [scrollView setContentOffset:point animated:YES];
    }

}
- (void)keyboardWasHidden:(NSNotification *)notif
{
    UIEdgeInsets zeroInsets = UIEdgeInsetsZero;
    UIScrollView *scrollView = (UIScrollView *)self.view;
    [scrollView setContentInset:zeroInsets];
    scrollView.scrollIndicatorInsets = zeroInsets;
}

最佳答案

试试这个:
self.automaticallyAdjustsScrollViewInsets = NO;
这对我有用...

10-08 12:18