How to resizing issue of UISearchController.searchBar when focused?

落花浮王杯 提交于 2020-01-16 19:49:13

问题


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?


回答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.



来源:https://stackoverflow.com/questions/55704967/how-to-resizing-issue-of-uisearchcontroller-searchbar-when-focused

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!