简体   繁体   English

UISearchBar作为键盘的一部分?

[英]UISearchBar as part of the keyboard?

I have a UISearchBar on a bottom toolbar. 我在底部的工具栏上有一个UISearchBar。 The problem is the keyboard covers it up once you click on it. 问题是,一旦您单击键盘,键盘就会将其盖住。 I was trying to think of ways around this. 我试图想办法解决这个问题。 I tried adding it to the top, but since I'm using a UINavigationController, I'm getting some unexpected behavior. 我尝试将其添加到顶部,但由于使用的是UINavigationController,因此出现了一些意外行为。 (Like the UISearchBar in the customView for the rightBarButtonItem lengthens once the keyboard is on screen and doesn't return to its original size, and the entire NavigationBar goes off the screen when the keyboard is pressed so you cannot even see what you are typing in that situation either.) (就像键盘在屏幕上并且没有恢复到原始大小时,rightBarButtonItem的customView中的UISearchBar会延长,当按下键盘时整个NavigationBar会离开屏幕,所以你甚至看不到你输入的是什么那种情况要么。)

So as a workaround, I was thinking I could have the systemIcon for search in the lower right corner of my toolbar, and then when the keyboard pops up, the searchbar could be part of the keyboard, and then you could see what you type. 所以作为一种解决方法,我想我可以在工具栏的右下角有用于搜索的systemIcon,然后当键盘弹出时,搜索栏可以成为键盘的一部分,然后你就可以看到你键入的内容了。 Does anyone know how I would do that? 有谁知道我会怎么做? Thanks. 谢谢。

From Apple's HIG for IOS: 从Apple的IOS HIG:

Display a search bar above a list or the index in a list. 在列表上方或列表中的索引上显示搜索栏。 Users expect to find a search bar in this position, because they're accustomed to the search bar in Contacts and other applications. 用户希望在此位置找到搜索栏,因为他们习惯于联系人和其他应用程序中的搜索栏。 Putting the search bar in this location means that it stays out of users' way when they're scrolling through the list or using the index, but is conveniently available when it's needed. 将搜索栏放在此位置意味着当用户滚动列表或使用索引时,它不会妨碍用户,但在需要时可以方便地使用。

Maybe showing a search bar at the bottom of the screen isn't meant to be ; 也许在屏幕底部显示一个搜索栏并不意味着; )

Placing the UISearchbar at the top in a custom ViewController should work. 将UISearchbar放在自定义ViewController顶部应该可以。 Maybe share some code? 也许共享一些代码?

Beryllium is on the right track. 铍在正确的轨道上。 here is some code that will help you get there. 这是一些代码,可以帮助您到达那里。

  1. Move the search bar off the screen so it cant be seen until the button is pushed 将搜索栏移出屏幕,直到按下按钮时才能看到它
  2. Register for keyboardWillShow notifications 注册keyboardWillShow通知
  3. when button is pushed, make the searchbar "becomeFirstResponder" 当按下按钮时,将搜索栏设为“ becomeFirstResponder”
  4. move the search bar from the bottom (out of view) to above the keyboard (in view) 将搜索栏从底部(视图外)移动到键盘上方(视图中)

// //

//in viewDidLoad register for notifications
 [self registerForKeyboardNotifications];

//in viewdidload hide the search bar
CGRect rect = searchBar.frame;
rect.origin.y = 480;
searchBar.frame = rect;

//own function register for keyboardwillshow
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}


//when button is pushed move the searchbar into view. you may have to play with the pixels a bit to make sure it lands on top, and flush

- (void)keyboardWillShow:(NSNotification*)aNotification
{

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
CGRect rect = searchBar.frame;
rect.origin.y = 180; //adjust the 180 to get it to land where you want
searchBar.frame = rect;
[UIView commitAnimations];
}


//own function hide the search bar when the keyboard is dismissed
- (void)keyboardWillHide:(NSNotification*)aNotification
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
CGRect rect = searchBar.frame;
rect.origin.y = 480;
searchBar.frame = rect;
[UIView commitAnimations];

}

I saw several solution for this task. 我看到了这个任务的几个解决方案。 In short, you creates an UISearchBar/UIToolbar, adds observer for UIKeyboardWillShowNotification and when this notificaion fires just animates you created UISearchBar/UIToolbar for 0.3 sec. 简而言之,你创建了一个UISearchBar / UIToolbar,添加了UIKeyboardWillShowNotification观察者,当这个通知触发动画时你创建了UISearchBar / UIToolbar 0.3秒。 Links: 链接:

  1. Programatically align a toolbar on top of the iPhone keyboard 以编程方式对齐iPhone键盘顶部的工具栏
  2. Building an iPhone keyboard toolbar 构建iPhone键盘工具栏
  3. Add toolbar on top of keyboard 在键盘顶部添加工具栏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM