2011-03-16 155 views
0

我想问一个基本的iPhone问题。我在iPhone视图中有很多TextField,当我点击在TextField中输入时,键盘显示并隐藏其他TextField。我想使父视图可滚动。你能给我看一下示例代码吗?iphone:在视图中滚动

非常感谢您

回答

2

您可以听取键盘上下通知。并将您的视图移动到键盘高度的上方。

ViewWillAppear方法:

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

ViewWillDisAppear方法:

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

,然后有方法上面提到调整杆的位置:

-(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; 
    } 
+0

类似谢谢你,但在这种情况下,视图不能滚动。如果我想向上/向下滚动查看其他文本框,我不能这样做。你有其他解决方案吗? – haisergeant 2011-03-16 09:44:52

+2

尝试将所有的视图放入UIScrollView(这将被称为容器视图)之后,在self.view addSubViews中添加UIScrollView:iMyUIScrollView ...让我知道任何困难... – Jhaliya 2011-03-16 09:48:30

+0

我将所有的TextField添加到UIScrollView该ScrollView是UIView(MyViewController)的子视图。但我仍然无法用许多TextField在该视图中滚动。我想我正在操纵UIScrollView,是吗? – haisergeant 2011-03-16 10:11:25

1

如果parentview是的UIScrollView然后尝试在文本框代表

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

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

}