我有一个 UISearchbar,它的键盘返回键是灰色的,除非我在搜索栏中键入文本。如何让它不出现在任何文本上?如果那不可能,如果搜索栏中没有文本,我该如何返回到我的 View ?
下面是一些代码:
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
touchtoCancel = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-211)];
[touchtoCancel addTarget:self
action:@selector(DidPressCancelSearch:)
forControlEvents:UIControlEventTouchUpInside];
[self.search setFrame:CGRectMake(self.search.frame.origin.x, self.search.frame.origin.y-210,
self.search.frame.size.width, self.search.frame.size.height)];
return YES;
}
-(IBAction)DidPressCancelSearch:(id)sender
{
[search resignFirstResponder];
}
最佳答案
除了已经提到的内置取消按钮之外的一种方法...您可以创建一个不可见的按钮,它占据键盘上方的区域,只有在编辑搜索栏时才会启用。点击这个不可见的按钮(搜索栏和键盘顶部之间的空间)应该让第一响应者辞职。
关于ios - UISearchbar 中没有文本时返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19441837/