ios - 当 UISplitViewController 模式全部可见时,UISearchController 搜索栏错位

标签 ios uisearchbar uisplitviewcontroller uisearchcontroller

我的 Root View Controller 是 UISplitViewControllerpreferredDisplayModeUISplitViewControllerDisplayModeAllVisible。在详细 View Controller 中,我有一个 UISearchController,但是我没有 UITableView 来附加搜索栏,所以我放置了一个 UIView在 View 中添加搜索栏:

_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_searchController.hidesNavigationBarDuringPresentation = NO;

// This is the view to contain search bar
_searchView.backgroundColor = [UIColor greenColor];
[_searchView addSubview:_searchController.searchBar];

self.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;

更新:UISearchBar 设置为 tableView.tableHeaderView 时也会发生这种情况,不仅是包含在 UIView 中时>.

这是它最初的样子(我裁剪了图像的高度):

enter image description here

但是当搜索 Controller 处于事件状态时,搜索栏框架的 origin.x 错位到右侧,正好等于主视图 Controller 的宽度:

enter image description here

当 Split View Controller 处于 UISplitViewControllerDisplayModePrimaryHidden 模式时,不会发生这种情况,搜索栏显示完全正常:

enter image description here

那么这是 UISplitViewController 的错误吗?目前我用这种丑陋的方式解决了这个问题,重置了搜索栏的 origin.x,有点小问题:

- (void)didPresentSearchController:(UISearchController *)searchController {
    searchController.searchBar.superview.clipsToBounds = NO;

    if (self.splitViewController.displayMode == UISplitViewControllerDisplayModeAllVisible) {
        searchController.searchBar.frame = ({
            CGRect frame = searchController.searchBar.frame;
            frame.origin.x -= self.splitViewController.primaryColumnWidth;
            frame;
        });
    }
}

最佳答案

这给我带来了很多麻烦。由于搜索栏在 SplitView 中放错了位置,我的应用程序可以在除 iPad 以外的所有设备上运行。我终于遇到了发布解决方案的人。将此添加到 TableView 的 viewDidLoad() 末尾:

self.definesPresentationContext = true

来源:searchBar from UISearchController not showing correctly on split view on iPad

关于ios - 当 UISplitViewController 模式全部可见时,UISearchController 搜索栏错位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41275318/

相关文章:

ios - 使用强制解包初始化 NSCalendar

iOS 11 tableView Header 中的搜索栏在焦点上跳转到屏幕顶部

ios - UITableViewController 通过方法添加项目

ios - UISplitViewController - 使用 Storyboard segues 替换详细 View Controller 是个好主意吗?

ios - 出现错误 tableView 的使用不明确(_ :numberOfRowsInSection:)

ios - 无法将 TableViewCell 类型的值转换为 'NSIndexPath'

swift - 如何通过 UISearchBar 文本过滤掉内容?

ios - 如何去除搜索栏背景?

ios - 无法在 SplitView 中覆盖 "preferredDisplayMode"

ios - 使用 setViewControllers 以编程方式导航