iPhone键盘隐藏UISeachBar [英] IPhone Keyboard hides UISeachBar

查看:120
本文介绍了iPhone键盘隐藏UISeachBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

您最好的选择可能是在UISearchBarDelegate协议中使用–searchBarShouldBeginEditing:.

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

它看起来像这样:

- (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是第一响应者时起作用.

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.

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

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.

这篇关于iPhone键盘隐藏UISeachBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆