Search a UISearchBar with no text in Xcode5 / iOS7

佐手、 提交于 2019-12-04 13:09:50
Adnan Aftab

In iOS 7 there is a small change, now you have to iterated two levels.

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

There is directly the option enablesReturnKeyAutomatically on search bar.

searchbar.enablesReturnKeyAutomatically = NO;

Here is a solution using Swift. Just paste it in your viewDidLoad function and make sure that you have an IBOutlet of your searchBar in the code. (on my example below, the inputSearchBar variable is the 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
            }
        }
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!