ios - 聚焦时如何调整 UISearchController.searchBar 的大小问题?
问题描述
我们很久以前就为 iOS 5 开发了一个应用程序,我们在其中使用了“搜索栏和搜索显示控制器”对象storyboard
。因为iOS8
UISearchDisplayController
是deprecated
,我正在尝试删除现有的“搜索栏和搜索显示控制器” UISearchController
。
由于UISearchController
界面上的“对象库”不可用,我已使用以下代码以编程方式将其添加到UIView
on中:interface
//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;
并将“searchController”的框架设置为等于 UIView,如下所示:
UISearchBar *ctrlSearchBar = self.searchController.searchBar;
ctrlSearchBar.frame = CGRectMake(0, 0, self.searchBarContainer.frame.size.width, self.searchBarContainer.frame.size.height);
searchBar 的大小很好,但是当它专注于点击它时,'searchBar' 的宽度会自动增加。
我什至尝试使用 NSLayoutConstraints 但没有解决问题。任何人都可以请帮助如何修复'UISearchController.searchBar'的大小,即使它是集中的?
解决方案
正如surToTheW在他的评论中所建议的那样,我创建了一个自定义 UIViewController,其中包含 UISearchController。我将它作为子视图控制器添加到我的主 UIViewController 中,并且搜索栏没有超过自定义 UIViewController 的视图宽度。
推荐阅读
- php - 在 laravel 中使用最大文件大小的验证错误
- javascript - 无法从 ajax 获得成功响应,并且在 firefox 和 IE 中提交时未定义 JQuery
- c# - 在守护程序中使用 Microsoft Graph API
- excel - Excel-引用包含引用动态范围的名称的单元格
- docker - Heroku 上 docker-compose 中的 Web 请求
- python - 如何使用任何类似于 R 的统计库在 python 中执行两个样本 t 测试?
- javascript - FreeCodeCamp (JavaScript) 欧拉项目 4
- pytorch - pyTorch DataLoader 被杀死
- python - 如何找到circusd的pid文件?
- arrays - Numpy, change array's row value , each row satisfy different conditions