0

我有一个滚动视图,它可以完美运行,直到我关闭键盘。之后它滚动但不再一直向下滚动......我有点困惑。

在 viewDidLoad 我保存了框架,以便以后可以重置它

frame = self.scrollView.frame;
NSLog(@"Height beginning: %f",self.scrollView.frame.size.height);
offset = self.scrollView.contentOffset;

为了调试,我检查了滚动视图的高度,即 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,这意味着滚动视图的高度已设置为旧值。它确实会滚动,但是当我放手时它会一直弹回开始...

编辑:

当使用 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;   
}
4

0 回答 0