旋转后UITableView标头中的UISearchBar布局混乱 [英] Layout of UISearchBar in UITableView header messed up after rotation
问题描述
我有一个UITableView
,其中搜索栏以编程方式插入到表的headerView
中:
I have a UITableView
with a search bar inserted programatically into the table's headerView
:
override func viewDidLoad() {
super.viewDidLoad()
resultSearchController = UISearchController(searchResultsController: nil)
resultSearchController.searchResultsUpdater = self
resultSearchController.dimsBackgroundDuringPresentation = false
resultSearchController.searchBar.delegate = self
resultSearchController.searchBar.placeholder = "Search Contacts"
resultSearchController.searchBar.scopeButtonTitles = ["All", "Title1", "Title2", "Title3"]
tableView.tableHeaderView = resultSearchController.searchBar
resultSearchController.searchBar.sizeToFit()
definesPresentationContext = true
tableView.tableFooterView = UIView(frame: CGRectZero)
}`
该表被设置为基于UINavigationController
的标准向下钻取-在详细视图控制器中选择行推送.
The table is set up as a standard UINavigationController
-based drill down - selecting a row pushes in a detail view controller.
所有内容都可以在人像或风景下完美工作,但是如果在显示细节视图时将其旋转,则在弹出细节视图后会分解.确切的顺序是:
Everything works perfectly in portrait or landscape, but breaks down after the detail view is popped if is has been rotated while displayed. The exact sequence is:
- 以纵向打开表格,使用搜索栏过滤结果,然后选择结果之一以推入详细视图
- 在局部视图中,旋转为横向
- 仍在局部视图中,旋转回纵向
重新显示表格视图时,搜索栏的所有单个元素都被折叠"在一起,并且整个内容覆盖了状态栏:
When the table view reappears, all the individual elements of the search bar have been "collapsed" on top of each other, and the whole thing overlays the status bar:
我尝试了多种方法在viewDidAppear
中显式设置搜索栏的框架,以及从标题视图中删除和替换它-但问题仍然存在.
I've tried various ways of explicitly setting the frame of the search bar in viewDidAppear
, as well as removing and replacing it from the header view - but the problem persists.
有人为此提出解决方法吗?
Had anyone come up with a workaround for this??
编辑后添加:
在进一步挖掘时,问题似乎在于旋转后导航栏消失了-在此之前:
On further digging, it looks like the problem is that the navigation bar disappears after the rotation - here's before:
及之后:
那么,有什么想法吗?
推荐答案
在注意到不活动的searchBar
似乎没有此问题之后,我成功地保存了视图消失并还原时的搜索栏状态在视图重新出现之前.
After noting that an inactive searchBar
doesn't seem to have this problem, I have had success with saving the search bar state when the view disappears and restoring it before the view reappears.
该示例代码在进行以下更改后可以正常工作.保存状态:
The sample code works fine with the following changes. Save the state:
- (void)viewDidDisappear:(BOOL)animated
{
if (self.searchController.active && self.searchController.searchBar.text.length > 0) {
self.savedSearch = self.searchController.searchBar.text;
[self disableSearch];
} else if (self.searchController.active) {
// empty search field - this won't get restored
[self disableSearch];
}
[super viewDidDisappear:animated];
}
- (void)disableSearch
{
if (self.searchController.isActive) {
self.searchController.searchBar.text = @"";
self.searchController.active = NO;
}
}
并恢复:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (self.savedSearch) {
NSLog(@"RESTORED SEARCH");
self.searchController.searchBar.text = self.savedSearch;
self.searchController.searchBar.showsCancelButton = YES;
self.searchController.active = YES;
self.savedSearch = nil;
}
}
无论是否设置self.searchController.hidesNavigationBarDuringPresentation
,这似乎都很好(尽管如果true
返回视图时会有一些动画).
This seems to work fine whether or not self.searchController.hidesNavigationBarDuringPresentation
is set (although if true
there is some animation on returning to the view).
这篇关于旋转后UITableView标头中的UISearchBar布局混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!