在Xcode5 / iOS7中搜索没有文本的UISearchBar

时间:2013-09-24 02:23:40

标签: uisearchbar ios7

我正在更新我的iOS 7应用程序和我的一个功能,允许激活搜索栏搜索按钮,搜索栏中没有文字停止工作。我使用了以下代码。关于如何让它再次运作的任何建议?提前谢谢。

UITextField *searchBarTextField = nil;
for (UIView *subview in self.searchBar.subviews)
{
    if ([subview isKindOfClass:[UITextField class]])
    {
        searchBarTextField = (UITextField *)subview;
        break;
    }
}
searchBarTextField.enablesReturnKeyAutomatically = NO;

3 个答案:

答案 0 :(得分:6)

在iOS 7中有一个很小的变化,现在你必须迭代两个级别。

  for (UIView *subView in self.searchBar.subviews){
    for (UIView *secondLeveSubView in subView.subviews){
    if ([secondLeveSubView isKindOfClass:[UITextField class]])
        {
            searchBarTextField = (UITextField *)2ndLeveSubView;
            break;
        }
    }
   }

答案 1 :(得分:4)

搜索栏上直接有选项enableReturnKeyAuto。

searchbar.enablesReturnKeyAutomatically = NO;

答案 2 :(得分:1)

这是使用Swift的解决方案。只需将其粘贴到viewDidLoad函数中,并确保代码中有searchBar的IBOutlet。 (在我下面的例子中,inputSearchBar变量是IBOutlet)

   // making the search button available when the search text is empty
    var searchBarTextField : UITextField!
    for view1 in inputSearchBar.subviews {
        for view2 in view1.subviews {
            if view2.isKindOfClass(UITextField) {
                searchBarTextField = view2 as UITextField
                searchBarTextField.enablesReturnKeyAutomatically = false
                break
            }
        }
    }