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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最好的选择可能是使用–SearchBarshouldBegineding:在UisearchBardelegate协议中。
看起来像这样:
编辑:其他答案之一建议使用Uikeyboard通知,但这可能会令人困惑。但是,它确实给出了每次出现键盘时工作的优势,而不仅仅是Uisearchbar是第一响应者。
编辑2:
Mayur Joshi建议使用动画,可以这样做:
编辑3:
如果您不想掩盖搜索栏后面的视图,则每当搜索栏移动时,都必须缩小其高度。它可以与代码相同的位置来动画搜索栏。
Your best bet is probably to use the –searchBarShouldBeginEditing: in the UISearchBarDelegate protocol.
It would look something like this :
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:
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.
你好,试试这个
sBar 是 UISearchBar 对象。
Hi, try this
sBar is UISearchBar object.
您将必须使用 UIScrollView 并将此搜索栏保留在该滚动视图中。现在,当您开始编辑搜索栏时,即为
UIScrollView设置scrollRectToVisible方法,以便使您的SearchBar像这样可见......
当您在搜索栏中写入文本时,即
设置滚动视图scrollRectToVisible 恢复到其原始大小。
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,
set the scrollRectToVisible method for the UIScrollView so as to make your SearchBar visible like this....
And when you have written the text in the search bar, i.e
set the Scroll view scrollRectToVisible to its original size.