iPhone:在视图中滚动

发布于 2024-10-22 11:32:44 字数 131 浏览 5 评论 0原文

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

非常感谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒儿 2024-10-29 11:32:44

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

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

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;
    }
娇女薄笑 2024-10-29 11:32:44

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

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

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

}

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; 

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文