我有一个完美的滚动视图,直到我解除键盘。在那之后它滚动但不是一直向下滚动...我有点困惑。
在viewDidLoad中我保存了帧,以便稍后重置
frame = self.scrollView.frame;
NSLog(@"Height beginning: %f",self.scrollView.frame.size.height);
offset = self.scrollView.contentOffset;
为了调试我检查了scrollview的高度629
在我的keyboardDidHide方法中我将旧框架设置回来
self.scrollView.frame = frame;
NSLog(@"Height end: %f",self.scrollView.frame.size.height);
// Reset the scrollview to previous location
self.scrollView.contentOffset = offset;
调试输出也是629,这意味着scrollview的高度已设置为旧值。它确实滚动,但是当我放开它时它会一直反弹到开头......
编辑:
当使用480作为高度时,由于iphone 4
,它不会填满整个屏幕
一些代码
-(void) keyboardDidShow: (NSNotification *)notif
{
NSLog(@"Keyboard is visible");
// If keyboard is visible, return
if (keyboardVisible) {
NSLog(@"Keyboard is already visible. Ignore notification.");
return;
}
// Get the size of the keyboard.
CGRect keyboardEndFrame;
[[notif.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];
// Resize the scroll view to make room for the keyboard
CGRect viewFrame = frame;
viewFrame.size.height = 200;
//viewFrame.size.height -= keyboardEndFrame.size.height;
self.scrollView.frame = viewFrame;
offset = self.scrollView.contentOffset;
CGRect textFieldRect;
if(!activeField)
textFieldRect = comment.frame;
else
textFieldRect = activeField.frame;
textFieldRect.origin.y += 10;
[self.scrollView scrollRectToVisible:textFieldRect animated:YES];
// Keyboard is now visible
keyboardVisible = YES;
}
-(void) keyboardDidHide: (NSNotification *)notif
{
// Is the keyboard already shown
if (!keyboardVisible) {
NSLog(@"Keyboard is already hidden. Ignore notification.");
return;
}
//viewFrame.size.height -= keyboardEndFrame.size.height;
self.scrollView.frame = frame;
[self.scrollView setContentSize:CGSizeMake(320, 700)];
// Reset the scrollview to previous location
self.scrollView.contentOffset = offset;
// Keyboard is no longer visible
keyboardVisible = NO;
}