0

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

非常感谢你

4

3 回答 3

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;
    }
于 2011-03-16T09:38:54.633 回答
2

这会给你一个想法

存在键盘时如何使 UITextField 向上移动?

于 2011-03-16T09:43:46.193 回答
1

如果父视图是 UIScrollView 然后尝试在文本字段委托中

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

    if (theTextField == textFieldName) {
        [scroll scrollRectToVisible:CGRectMake(0, 160, 280, 440) animated:YES];//相应地选择矩形。
    }
    返回是;

}
于 2011-03-16T09:51:41.917 回答