ios - UISearchbar 中没有文本时返回

标签 ios cocoa-touch uisearchbar

我有一个 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/

相关文章:

ios - 使用动画生成插值

ios - x 轴下方的刻度线(iOS 图表)

objective-c - iOS 多人游戏

ios - 从 'String' 转换为不相关类型 'LocationModel' 始终失败警告

ios - 无法在 UIStackView 中设置按钮图像

iphone - 如何从嵌套的 UIViewController 中获取正确的 UIInterfaceOrientation

iphone - 获取经度和纬度值

ios - 带有 UITableViewController 的 SearchBar 包含错误数量的单元格

iphone - 搜索激活后 IOS7 状态栏变为黑色

ios - SpriteKit 球在撞墙时失去所有能量,恢复=1