Search code examples
iphoneviewuiscrollviewuiscrollviewdelegate

iphone: scroll in a view


I would like to ask a basic iphone question. I have many TextFields in iPhone view, when I tap to input in a TextField, the Keyboard shows up and hide other TextField. I would like to make the parent view scrollable. Would you please show me the example code?

Thank you very much


Solution

  • You can listen for the keyboard up and down notification. and move your view just above height of keyboard.

    In the ViewWillAppear method:

    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self selector:@selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil];
    [nc addObserver:self selector:@selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object:nil];
    

    In the ViewWillDisAppear method:

      [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 
    

    And then have methods referred to above to adjust the position of the bar:

    -(void) keyboardWillShow:(NSNotification *) note
    {
        CGRect r  = bar.frame, t;
        [[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &t];
        r.origin.y -=  t.size.height;
        bar.frame = r;
    }
    
    
    
    
     -(void) keyboardWillHide:(NSNotification *) note
        {
            CGRect r  = bar.frame, t;
            [[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &t];
            r.origin.y +=  t.size.height;
            bar.frame = r;
        }