2016-04-06 53 views
3

看起來像一個常見的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]; 

的問題是,只要在搜索欄被觸摸,鍵盤彈出,但整個搜索欄中消失。

我看着這些帖子:

,並試圖每一個解決方案,最終將這個初始化代碼:

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 

回答

1

有一對夫婦的事情,我會建議。

  1. 在viewDidLoad中

    // after creating the searchController add 
    self.mySearchController.active = YES; 
    
    // also add this for the search bar 
    [self.mySearchController.searchBar setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; 
    

我成立了一個簡單的測試應用程序。頂部添加了一個UIView(這將是searchBarWrapper)。受限的頂部,領先,尾隨,並給它一個固定的高度。在下面添加了一個tableView,使用prototypeCell並將它限制爲Top,Leading,Trailing,Bottom ---簡單簡單,沒有什麼特別之處。所有的作品都很好。

我可以發佈整個viewControllerClass,如果你需要它。

+0

沒有爲我工作,但也許是由於我的視圖層次結構中的東西。我用它更新了這個問題 – Cbas

+0

啊,是的,從一開始就會有幫助。有一個錯誤,其中UISearchController沒有正確處理呈現UISearchController的視圖控制器的視圖從其超級視圖中插入的情況。這可能是您面臨的問題的一部分。 – MDB983

0

我有同樣的確切問題。對我來說固定的是在searchBarWrapper中添加錨點,然後在viewDidLayoutSubviews中再次添加錨點。基本上我不得不添加他們兩次:

@IBOutlet weak var searchBarWrapper: UIView! 
fileprivate var searchController: UISearchController! 

//MARK:- View Controller Lifecycle 
override func viewDidLoad() { 
     super.viewDidLoad() 

     setSearchController() 
} 

override func viewDidLayoutSubviews() { 
     super.viewDidLayoutSubviews() 

     positionSearchControllerInWrapperView() 
} 

//MARK:- Custom Funcs 
fileprivate func setSearchController(){ 
     searchController = UISearchController(searchResultsController: nil) 
     searchController.delegate = self 
     searchController.searchBar.delegate = self 
     searchController.searchResultsUpdater = self 
     searchController.searchBar.showsCancelButton = false 
     searchController.searchBar.placeholder = "Search man..." 
     searchController.searchBar.returnKeyType = .done 
     searchController.dimsBackgroundDuringPresentation = false 
     searchController.hidesNavigationBarDuringPresentation = false 
     searchController.searchBar.endEditing(true) 
     searchController.searchBar.sizeToFit() 

     definesPresentationContext = true 
     navigationItem.hidesBackButton = true 

     positionSearchControllerInWrapperView() 
} 

//this gets called TWICE 
fileprivate func positionSearchControllerInWrapperView(){ 
     searchBarWrapper.addSubview(searchController.searchBar) 
     searchController.searchBar.translatesAutoresizingMaskIntoConstraints = false 
     searchController.searchBar.leftAnchor.constraint(equalTo: searchBarWrapper.leftAnchor).isActive = true 
     searchController.searchBar.rightAnchor.constraint(equalTo: searchBarWrapper.rightAnchor).isActive = true 
     searchController.searchBar.topAnchor.constraint(equalTo: searchBarWrapper.topAnchor).isActive = true 
     searchController.searchBar.bottomAnchor.constraint(equalTo: searchBarWrapper.bottomAnchor).isActive = true 
} 
相關問題