Objective c UIScrollView返回其原始位置
我有以下代码:Objective c UIScrollView返回其原始位置,objective-c,xcode,uiscrollview,Objective C,Xcode,Uiscrollview,我有以下代码: float yOffset = activeTextView.frame.origin.y - keyboardSize.height + 55; CGPoint scrollPoint = CGPointMake(0.0, yOffset); [scrollView setContentOffset:scrollPoint animated:YES]; 这将在-(void)键盘显示:(NSNotification*)通知中设置滚动视图的动画 隐
float yOffset = activeTextView.frame.origin.y - keyboardSize.height + 55;
CGPoint scrollPoint = CGPointMake(0.0, yOffset);
[scrollView setContentOffset:scrollPoint animated:YES];
这将在-(void)键盘显示:(NSNotification*)通知中设置滚动视图的动画
隐藏键盘后,我尝试将scrollView返回到其原始位置,如下所示:
- (void) keyboardWillHide:(NSNotification *)notification {
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
但它不起作用
如何将UIScrollView和整个屏幕返回到其原始位置,以便用户看到在scrollview动画之前看到的内容?在键盘显示:
方法中,您正在设置contentOffset
属性([scrollview setContentOffset:]
相当于scrollView.contentOffset
)。但是,在keyboardWillHide:
中,您正在设置contentInset
,这是完全不同的(本质上是滚动视图内容的内部填充量)。试一试
或
另外,正如NSResponder提到的,请确保调用了键盘willhide:
方法。我看不到您注册UIKeyboardWillHideNotification的任何代码。如何调用您的-keyboardWillHide:方法?我在viewDidLoad[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector中注册(KeyboardWasShowed:)名称:UIKeyboardDidShowNotification对象:nil];[[NSNotificationCenter defaultCenter]添加观察者:自选择器:@selector(keyboardWillHide:)名称:UIKeyboardWillHideNotification对象:nil];这不起作用,因为它还会将scrollView滚动到顶部。这就是此代码的设计目的-此类性质的问题通常具有scrollView的未滚动位置,指示屏幕通常应该是什么样子,并在打开键盘时移动所有内容。如果您还将滚动视图
用于其他行为,并希望它返回到上一个位置,请将contentOffset
值存储到CGPoint
类属性或变量中,并在隐藏键盘时将我答案中的CGPointZero
替换为该变量。
scrollView.contentOffset = CGPointZero; // non-animated by default
[scrollView setContentOffset:CGPointZero animated:YES]; // animated