看起來像一個常見的iOS錯誤,但我已經嘗試了所有可找到的解決方案,並且沒有看到任何結果。UISearchController - 搜索欄在觸摸時從包裝中消失
我創建一個UISearchController
並將其添加到UIView
包裝是這樣的:
self.searchController = [[SearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.delegate = self;
[self.searchBarWrapper addSubview:self.searchController.searchBar];
[self.searchController.searchBar sizeToFit];
的問題是,只要在搜索欄被觸摸,鍵盤彈出,但整個搜索欄中消失。
我看着這些帖子:
- My UISearchBar in UISearchController disappear when I start searching. Why?
- iOS 9 searchBar disappears from table header view when UISearchController is active
- UISearchController searchBar disappears on first click
- Disappearing UISearchController in a TableViewController that is in a UINavigationController
- UITableView disappears when UISearchController is active and a new tab is selected
- Search Bar disappears after tap on it
- UISearchController searchBar in tableHeaderView animating out of the screen
- search bar getting disappeared in ios UIsearchcontroller
- Search Bar disappeared from view while typing
,並試圖每一個解決方案,最終將這個初始化代碼:
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.definesPresentationContext = NO;
self.navigationController.definesPresentationContext = YES;
self.navigationController.extendedLayoutIncludesOpaqueBars = YES;
self.extendedLayoutIncludesOpaqueBars = YES;
而且這些委託功能
- (void)willPresentSearchController:(UISearchController *)searchController {
// definitely runs, if I put a breakpoint here it stops
self.navigationController.navigationBar.translucent = YES;
[self.searchBarWrapper addSubview:self.searchController.searchBar];
searchController.searchResultsController.view.hidden = NO;
}
-(void)willDismissSearchController:(UISearchController *)searchController{
self.navigationController.navigationBar.translucent = NO;
searchController.searchResultsController.view.hidden = NO;
}
但搜索欄還是消失了恩,感動。我還能嘗試什麼?
我認爲我的代碼和其他帖子之間的區別在於,我使用不帶XIB的編程創建的UIViewController
。有一個UITableView
子視圖,但它不直接與搜索欄進行交互。沒有UINavigationBar
,搜索欄位於視圖的頂部。
UPDATE:
這是我的看法相對於搜索欄層次:
- UIViewController
- UIView
- UIScrollView
- UIViewController
- UIView
- UISearchBar
沒有爲我工作,但也許是由於我的視圖層次結構中的東西。我用它更新了這個問題 – Cbas
啊,是的,從一開始就會有幫助。有一個錯誤,其中UISearchController沒有正確處理呈現UISearchController的視圖控制器的視圖從其超級視圖中插入的情況。這可能是您面臨的問題的一部分。 – MDB983