Search code examples
iphoneiosuisearchbaruisearchdisplaycontroller

Elegant way to swap keyboard for UIDatePicker in UISearchBar and UISearchDisplayController


I know about this way UISearchBar with InputView

But I want to know if there is a "proper" way to do this at all? A better way than what appears to be a hack above, and there is obviously no guarantee that that will work in future versions of the iOS SDK.

I have a search bar, with a segmented control (scopes) and on one of the scopes I want to display a date picker instead of the keyboard. The inputView property is not accessible for a standard UISearchBar, so is there some other way of doing this?

Many thanks

Tom


Solution

  • Only in 2 Points, First Track the selected Scope Secondly, Whenever your desired scope is selected then Resign the Keyboard and show the Picker. Maybe this code will help you.

    #pragma mark - UISearchBarDelegate
    - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{
        // Track the Scope Selection    
        selectedSearchType = selectedScope ;
    }
    
    
    - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    
       if(selectedSearchType=="Scope_which_will_expose_picker"){
         // Show Picker Here
        [searchBar resignFirstResponder];
       }
        return YES;
    }