首页 > 解决方案 > 聚焦时如何调整 UISearchController.searchBar 的大小问题?

问题描述

我们很久以前就为 iOS 5 开发了一个应用程序,我们在其中使用了“搜索栏和搜索显示控制器”对象storyboard。因为iOS8 UISearchDisplayControllerdeprecated,我正在尝试删除现有的“搜索栏和搜索显示控制器” UISearchController

由于UISearchController界面上的“对象库”不可用,我已使用以下代码以编程方式将其添加到UIViewon中: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'的大小,即使它是集中的?

标签: iosobjective-cxcodeuisearchcontroller

解决方案


正如surToTheW在他的评论中所建议的那样,我创建了一个自定义 UIViewController,其中包含 UISearchController。我将它作为子视图控制器添加到我的主 UIViewController 中,并且搜索栏没有超过自定义 UIViewController 的视图宽度。


推荐阅读