iphone: scroll in a view

别来无恙 提交于 2019-12-21 03:21:13

问题


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


回答1:


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



回答2:


This will give you an idea

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




回答3:


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; 

}


来源:https://stackoverflow.com/questions/5323274/iphone-scroll-in-a-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!