搜索栏与状态栏重叠 iOS 11

Search bar overlaps with status bar on iOS 11

我正在使用 UISearchController 和 UISearchResultsController 来实现搜索功能。

MySearchResultsController 实现 UISearchResultsUpdating 和 UISearchBarDelegate:

override open func viewDidLoad() {
    super.viewDidLoad()
    self.edgesForExtendedLayout = [];
    self.automaticallyAdjustsScrollViewInsets = false;
}

我在 MyTableViewController 中像这样在 tableHeader 中显示搜索栏:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultsController];
    self.searchController.searchResultsUpdater = self.searchResultsController;
    self.searchController.searchBar.delegate = self.searchResultsController;
    self.searchController.searchBar.scopeButtonTitles = @[NSLocalizedString(@"SEARCH_SCOPE_TEMPERATURES", nil), NSLocalizedString(@"SEARCH_SCOPE_KNOWHOW", nil)];
    self.tableView.tableHeaderView = self.searchController.searchBar;
    self.definesPresentationContext = YES;
}

这在以前工作得很好,但在 iOS 11 下,只要我点击它,搜索栏就会与状态栏重叠(见屏幕截图)。我尝试了很多不同的方法来让它正确显示,但还没有找到解决方案。

我设法通过子类化 UISearchController 解决了这个问题。我的答案在 Swift 中,但也许这些原则也适用于 ojective-c。请在这里查看我的回答:

我发现问题是呈现视图控制器也设置了

override open func viewDidLoad() {
    super.viewDidLoad()
    self.edgesForExtendedLayout = [];
    self.automaticallyAdjustsScrollViewInsets = false;
}

我必须这样做,因为 table 视图实际上并没有一直延伸到顶部。

我在呈现视图控制器中这样解决了这个问题:

override open func viewDidLoad() {
    super.viewDidLoad()
    self.automaticallyAdjustsScrollViewInsets = false;
    if (@available(iOS 11.0, *)) {
        //NSLog(@"iOS 11.0");
    } else {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        //NSLog(@"iOS < 11.0");
    }
}

似乎是一个 iOS 11 错误,或者至少是一个奇怪的行为......

这对我有用:

    override func viewDidLoad() {
        // to fix the Status Bar Issue:
        if #available(iOS 11.0, *) {
            definesPresentationContext = true
        }
       // You'll also need this properties on your Search Bar:
        searchController = UISearchController.init(searchResultsController: nil)
        searchController?.searchResultsUpdater = self
        searchController?.hidesNavigationBarDuringPresentation = false
    }