iPhone键盘隐藏了Uiseachbar

发布于 2024-12-05 07:01:16 字数 174 浏览 1 评论 0原文

我在视图底部有一个搜索栏。问题是每当我在键盘中输入内容时,搜索栏仍然保留在底部,我无法查看我正在输入的内容。我想在键盘弹出时动态地将其向上移动,然后在键盘消失后收回其原始位置。不幸的是,搜索栏必须位于我的应用程序的底部,并且有点卡在这个位置。

有什么方法可以仅在键盘出现时向上移动搜索栏吗?任何代码片段都会非常有帮助。

I have a search bar at the bottom of a view. The issue is whenever I type something in the keyboard, the search bar still remains at the bottom and I cannot view what I am typing. I would like to dynamically move it up whenever the keyboards pops up and then take back its original position after the keyboard disappears. And unfortunately the search bar has to be in the bottom for my app and kind of stuck in this.

Is there any way around to move the search bar up only when the keyboard appears? Any code snippets would be really helpful.

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

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

发布评论

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

评论(3

避讳 2024-12-12 07:01:17

最好的选择可能是使用–SearchBarshouldBegineding:在UisearchBardelegate协议中。

看起来像这样:

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    CGRect newFrame = //some rect
    mySearchBar.frame = newFrame;
    return YES;//this is important!
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    CGRect originalFrame = //the original frame
    mySearchBar.frame = originalFrame;
    return YES;
}

编辑:其他答案之一建议使用Uikeyboard通知,但这可能会令人困惑。但是,它确实给出了每次出现键盘时工作的优势,而不仅仅是Uisearchbar是第一响应者。

编辑2:
Mayur Joshi建议使用动画,可以这样做:

[UIView animateWithDuration:duration
            animations:^{ 
                //what you want to animate (in this case, the search bar's frame)
            } 
            completion:^(BOOL finished){
                //what to do when the animation finishes
            }];

编辑3:
如果您不想掩盖搜索栏后面的视图,则每当搜索栏移动时,都必须缩小其高度。它可以与代码相同的位置来动画搜索栏。

Your best bet is probably to use the –searchBarShouldBeginEditing: in the UISearchBarDelegate protocol.

It would look something like this :

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    CGRect newFrame = //some rect
    mySearchBar.frame = newFrame;
    return YES;//this is important!
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
    CGRect originalFrame = //the original frame
    mySearchBar.frame = originalFrame;
    return YES;
}

EDIT: One of the other answers suggests using the UIKeyboard notifications, but that can be confusing. It does, however, give the advantage of working for every time the keyboard appears, rather than just when the UISearchBar is the first responder.

EDIT 2:
Mayur Joshi suggests using animation, which can be done like so:

[UIView animateWithDuration:duration
            animations:^{ 
                //what you want to animate (in this case, the search bar's frame)
            } 
            completion:^(BOOL finished){
                //what to do when the animation finishes
            }];

EDIT 3:
If you don't want to obscure the view behind the search bar, you will have to shrink its height whenever the search bar moves up. It can go in the same place as the code to animate your search bar.

请远离我 2024-12-12 07:01:17

你好,试试这个
sBar 是 UISearchBar 对象。

    - (void)keyboardWillShow:(NSNotification *)aNotification 
    {
        // the keyboard is showing so resize the table's height
        CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        NSTimeInterval animationDuration =
        [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        CGRect frame = sBar.frame;
        NSLog(@"%f",frame.size.height);
        NSLog(@"%f",frame.origin.y);
        frame.origin.y=frame.origin.y- keyboardRect.size.height;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        sBar.frame = frame;
        NSLog(@"%f",frame.size.height);
        NSLog(@"%f",frame.origin.y);
        [UIView commitAnimations];
    }

    - (void)keyboardWillHide:(NSNotification *)aNotification
    {
        // the keyboard is hiding reset the table's height
        CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        NSTimeInterval animationDuration =
        [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        CGRect frame = sBar.frame;
        NSLog(@"%f",frame.size.height);
        NSLog(@"%f",frame.origin.y);
        frame.origin.y=frame.origin.y+ keyboardRect.size.height;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        sBar.frame = frame;
        NSLog(@"%f",frame.size.height);
        NSLog(@"%f",frame.origin.y);

        [UIView commitAnimations];
    }

Hi, try this
sBar is UISearchBar object.

    - (void)keyboardWillShow:(NSNotification *)aNotification 
    {
        // the keyboard is showing so resize the table's height
        CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        NSTimeInterval animationDuration =
        [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        CGRect frame = sBar.frame;
        NSLog(@"%f",frame.size.height);
        NSLog(@"%f",frame.origin.y);
        frame.origin.y=frame.origin.y- keyboardRect.size.height;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        sBar.frame = frame;
        NSLog(@"%f",frame.size.height);
        NSLog(@"%f",frame.origin.y);
        [UIView commitAnimations];
    }

    - (void)keyboardWillHide:(NSNotification *)aNotification
    {
        // the keyboard is hiding reset the table's height
        CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
        NSTimeInterval animationDuration =
        [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        CGRect frame = sBar.frame;
        NSLog(@"%f",frame.size.height);
        NSLog(@"%f",frame.origin.y);
        frame.origin.y=frame.origin.y+ keyboardRect.size.height;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        sBar.frame = frame;
        NSLog(@"%f",frame.size.height);
        NSLog(@"%f",frame.origin.y);

        [UIView commitAnimations];
    }
深陷 2024-12-12 07:01:17

您将必须使用 UIScrollView 并将此搜索栏保留在该滚动视图中。现在,当您开始编辑搜索栏时,即为

 - (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar

UIScrollView设置scrollRectToVisible方法,以便使您的SearchBar像这样可见......

[yourScrollView scrollRectToVisible:CGRectMake(0, 300, yourScrollView.frame.size.width, yourScrollView.frame.size.height) animated:YES];    

当您在搜索栏中写入文本时,即

 - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar1

设置滚动视图scrollRectToVisible 恢复到其原始大小。

[yourScrollView scrollRectToVisible:CGRectMake(0, 0, yourScrollView.frame.size.width, yourScrollView.frame.size.height) animated:YES];  

You will have to use a UIScrollView and keep this search bar in that scroll View. Now, when you start editing the Search Bar, that is,

 - (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar

set the scrollRectToVisible method for the UIScrollView so as to make your SearchBar visible like this....

[yourScrollView scrollRectToVisible:CGRectMake(0, 300, yourScrollView.frame.size.width, yourScrollView.frame.size.height) animated:YES];    

And when you have written the text in the search bar, i.e

 - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar1

set the Scroll view scrollRectToVisible to its original size.

[yourScrollView scrollRectToVisible:CGRectMake(0, 0, yourScrollView.frame.size.width, yourScrollView.frame.size.height) animated:YES];  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文