开发者

UIScrollView scrolling issue

开发者 https://www.devze.com 2023-04-12 18:56 出处:网络
I have a scrollview which works perfectly until I dismiss the keyboard. After that it scrolls but not all the way down anymore... I am a bit puzzled.

I have a scrollview which works perfectly until I dismiss the keyboard. After that it scrolls but not all the way down anymore... I am a bit puzzled.

in viewDidLoad I save the frame so I can reset it later

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

For debugging I checked the scrollview's height which is 629

in my keyboardDidHide method I set the old frame back

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

// Reset the scrollview to previous location
self.scrollView.contentOffset = offset;

The debugging output is 629 as well which means that the scrollview's height has been set to the old value. It does scroll but when I let go it bounces all the way back to the beginning...

EDIT:

When using 480 as height it is not filling the whole screen because of iphone 4

UIScrollView scrolling issue

Some code

-(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;

    // Ke开发者_JS百科yboard is no longer visible
    keyboardVisible = NO;   
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号