2011-03-16 105 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; 

}