0

We have developed an application for iOS 5 long back where we used 'Search Bar and Search Display Controller' object on storyboard. Since iOS8 UISearchDisplayController is deprecated, I am trying to remove existing 'Search Bar and Search Display Controller' with UISearchController.

As UISearchController is not available from 'Object Library' on interface, I have added it programatically to an UIView on interface using following code:

//Set up Search Controller
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = (id)self;
self.searchController.dimsBackgroundDuringPresentation = YES;
self.searchController.searchBar.delegate = self;

//Adding as subview to UIView 'searchBarContainer'
[self.searchBarContainer addSubview:self.searchController.searchBar];
[self.searchBarContainer bringSubviewToFront:self.searchController.searchBar];

self.definesPresentationContext = YES;
self.extendedLayoutIncludesOpaqueBars = YES;

And set the frame of 'searchController' equal to the UIView as follows:

UISearchBar *ctrlSearchBar = self.searchController.searchBar;
ctrlSearchBar.frame = CGRectMake(0, 0, self.searchBarContainer.frame.size.width, self.searchBarContainer.frame.size.height);

The size of searchBar is fine but when it is focused on clicking on it, the width of 'searchBar' increased automatically.

I even tried using NSLayoutConstraints but didn't fix the issue. Can anyone please help how to fix the size of 'UISearchController.searchBar' even when it is focused?

D Surya Praveen
  • 321
  • 3
  • 17
  • Do you have a `UITableView` for the results? If yes - can you set the search controller as a `tableHeaderView` there? Another option would be to use directly `UISearchBar` without `UISearchController`. – surToTheW Apr 16 '19 at 14:21
  • @surToTheW Even when setting UISearchController to tableHeaderView I faced the same sizing issue with search bar when it is focused. Search Bar's width is exceeding the device width. And about directly using UISearchBar I think the only delegate we can use is textDidChange. Is textDidChange preferable instead of using UISearchController's 'updateSearchResults' delegate? I don't have idea but many developers are suggesting to use UISearchController only when it is required to use UISearchBar with UITableView. – D Surya Praveen Apr 17 '19 at 04:54
  • 1
    I tried it and when having the width of the table view less than the width of the screen the search bar was behaving strangely - exceeding the table view width or sometimes moving up, but staying in the view controller size. So I made a child view controller to the current one, set its width to less than the screen size and added the table view and search controller there. If this is an option for you, you must set `self.definesPresentationContext = YES;` and also the search bar was set as tableHeaderView like this: `self.tableView.tableHeaderView = self.searchController.searchBar;` – surToTheW Apr 17 '19 at 08:26
  • @surToTheW Great!! This might be the solution. I will check and come back to you. – D Surya Praveen Apr 17 '19 at 12:43
  • 1
    @surToTheW Worked like a charm!! Thank you very much! I don't know how to mark your response as answer. But I believe this is the correct answer. – D Surya Praveen Apr 19 '19 at 07:00

2 Answers2

1

As surToTheW suggested in his comment, I have created a custom UIViewController with UISearchController in it. I added it as child view controller in to my main UIViewController and the search bar didn't exceed the custom UIViewController's view width.

D Surya Praveen
  • 321
  • 3
  • 17
0

In the context of a searchBar in the primary column of a UISplitViewController the searchBar text field has a roughly double the width of the column. User typing is invisible until about 10 chars are entered. The issue can be avoided by using the iOS10 method of installing the searchBar. Use the iOS10 and earlier method of installing the searcher as follows

tableView.tableHeaderView = searchController.searchBar

Here's the 'new' iOS 11 and later approach that seems to have the primary column too wide and that in turn makes the searchText field too big.

navigationItem.searchController = searchController

You can use the Apple Sample App "Displaying Searchable Content by Using a Search Controller" and review the MainTableViewController.viewDidLoad() for an example of the two approaches. Add a UISplitViewController to the sample app to see the width issue.

A feedBackAssistant report has been filed on this issue - See FB9515194

iPadOS 14.7.1. iPad landscape orientation so both primary and secondary columns of the UISplitViewController are displayed.