2012-09-10 26 views
0

我有以下代码:的UIScrollView回到它原来的地方

float yOffset = activeTextView.frame.origin.y - keyboardSize.height + 55; 
     CGPoint scrollPoint = CGPointMake(0.0, yOffset); 
     [scrollView setContentOffset:scrollPoint animated:YES]; 

这个动画滚动视图中- (void)keyboardWasShown:(NSNotification *)notification

我想了滚动回到它原来的位置后,隐藏键盘这样的:

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

    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
    scrollView.contentInset = contentInsets; 
    scrollView.scrollIndicatorInsets = contentInsets; 

} 

但它不起作用!

我怎样才能返回UIScrollView和实际上整个屏幕到其原始位置,使用户将看到他看到什么之前,滚动视图的动画?

+0

我没有看到你注册UIKeyboardWillHideNotification的任何代码。你的-keyboardWillHide:方法如何被调用? – NSResponder

+0

我在viewDidLoad中注册[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown :) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide :) name:UIKeyboardWillHideNotification object:nil]; – Dejell

回答

2

在您的keyboardWasShown:方法中,您正在设置contentOffset属性([scrollView setContentOffset:]相当于scrollView.contentOffset)。但是,在keyboardWillHide:中,您设置的是contentInset,这是完全不同的(本质上,它是滚动视图内容的内部填充量)。尝试

scrollView.contentOffset = CGPointZero; // non-animated by default 

[scrollView setContentOffset:CGPointZero animated:YES]; // animated 

而且,NSResponder类提到的,请确保您的keyboardWillHide:方法被调用。

+0

完美。谢谢 – Dejell

+0

这不起作用,因为它也将scrollView滚动到顶部。 –

+0

这就是代码的设计目的 - 这种性质的问题通常具有scrollView的未滚动位置,指示屏幕通常应该显示的样子,并且在打开键盘时移动所有内容。如果您还在为其他行为使用'scrollView'并希望它返回到前一个位置,请将'contentOffset'值存储到'CGPoint'类属性或变量中,并将我的答案中的'CGPointZero'替换为该变量当你隐藏键盘。 – Xono