Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search a UISearchBar with no text in Xcode 5 / iOS 7

I'm updating my app for iOS 7 and one of my functions that allowed a search bar search button to be activated with no text in the search bar stopped working. I used the following code. ANy suggestions on how to make it work again? Thanks in advance.

UITextField *searchBarTextField = nil;
for (UIView *subview in self.searchBar.subviews)
{
    if ([subview isKindOfClass:[UITextField class]])
    {
        searchBarTextField = (UITextField *)subview;
        break;
    }
}
searchBarTextField.enablesReturnKeyAutomatically = NO;
like image 691
user1681673 Avatar asked Dec 19 '22 23:12

user1681673


2 Answers

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

  for (UIView *subView in self.searchBar.subviews){
    for (UIView *secondLeveSubView in subView.subviews){
    if ([secondLeveSubView isKindOfClass:[UITextField class]])
        {
            searchBarTextField = (UITextField *)2ndLeveSubView;
            break;
        }
    }
   }
like image 81
Adnan Aftab Avatar answered Jan 04 '23 07:01

Adnan Aftab


There is directly the option enablesReturnKeyAutomatically on search bar.

searchbar.enablesReturnKeyAutomatically = NO;
like image 24
CarmenA Avatar answered Jan 04 '23 06:01

CarmenA