Reputation: 341
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?
Upvotes: 0
Views: 2135
Reputation: 278
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.
Upvotes: 1
Reputation: 341
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.
Upvotes: 1