1

I am using UISearchController programmatically, While tapping on Searchbar textField I want to be set Date picker as inputView

@property (strong, nonatomic) UISearchController *searchController;

References for text field : iPhone display date picker on UITextField touch
But unable to able to access Search bar Textfield. Please help.

Community
  • 1
  • 1
Joyal Clifford
  • 1,212
  • 11
  • 20

2 Answers2

1

You can get search textField using following code. Write it in viewDidLoad

UITextField *searchBarTextField = [self.searchController.searchBar valueForKey:@"_searchField"];
[searchBarTextField setInputView:datePicker];

Hope this will help you

Nirav D
  • 71,513
  • 12
  • 161
  • 183
0

First set delegate of UISearchBar then you can use following delegate method :

- (BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar {
    [self showDatePicker];
    return NO;
}

-(void) showDatePicker {
    UIDatePicker *datePicker = [[UIDatePicker alloc]init];
    datePicker.datePickerMode = UIDatePickerModeDate;
    [datePicker setDate:[NSDate date]];
    [datePicker addTarget:self action:@selector(showSelectedDate:) forControlEvents:UIControlEventValueChanged];
}

- (void)showSelectedDate:(id)sender {
    UIDatePicker *picker = (UIDatePicker*)self.date.inputView;
    searchBar.text = [self formatDate:picker.date];
}

You can use any date formatter to show date in desired format.

Pushkraj Lanjekar
  • 2,254
  • 1
  • 21
  • 34
  • - (BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar { [self showDatePicker]; return NO; If NO nothink will display. If YES Datepicker and Keyboard both will display } – Joyal Clifford Jun 20 '16 at 10:56
  • Option 1 : Keep return YES in searchBarShouldBeginEditing and in 'showDatePicker' write [searchBar resignFirstResponder]; Option 2 : Add tap gesture on UISearchBar, write @selector for same. And in that gesture method, call 'showDatePicker' method. In this case, don't set delegate for UISearchBar. I think this will work. – Pushkraj Lanjekar Jun 20 '16 at 11:03