0

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

haisergeant
  • 1,081
  • 2
  • 13
  • 31

3 Answers3

2

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;
    }
Jhaliya - Praveen Sharma
  • 31,294
  • 8
  • 69
  • 74
  • Thank you, but in this case the view can not scroll. If I would like to scroll up/down to look at other textfield, I can not do that by this way. Do you have other solutions? – haisergeant Mar 16 '11 at 09:44
  • 2
    Try with putting all your views inside UIScrollView (This will be called container view) after that add tis UIScrollView in self.view addSubViews:iMyUIScrollView ... let me know for any difficulty... – Jhaliya - Praveen Sharma Mar 16 '11 at 09:48
  • I add all the TextField to a UIScrollView, that ScrollView is a sub view of UIView (MyViewController). But I still can not scroll the view with many TextField in that. I think I am manipulating with UIScrollView, is that right? – haisergeant Mar 16 '11 at 10:11
  • 1
    Now, could you now see your view on top of Keyboard ? – Jhaliya - Praveen Sharma Mar 16 '11 at 10:13
  • 1
    Use the apple sample code for working with scrollview's http://developer.apple.com/library/ios/#samplecode/Scrolling/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008023 – Jhaliya - Praveen Sharma Mar 16 '11 at 10:15
2

This will give you an idea

How to make a UITextField move up when keyboard is present?

Community
  • 1
  • 1
visakh7
  • 26,280
  • 8
  • 53
  • 69
1

If the parentview is UIScrollView then try something like in textfield delegate

- (BOOL) textFieldShouldReturn:(UITextField *)theTextField
{   

    if (theTextField == textFieldName)  { 
        [scroll scrollRectToVisible:CGRectMake(0, 160, 280, 440) animated:YES];//choose the rect accordingly.
    }
    return YES; 

}
pradeepa
  • 3,986
  • 5
  • 27
  • 41