UISearchController - 触摸时搜索栏从包装器中消失

4
似乎是一个常见的iOS bug,但我尝试了所有能找到的解决方法,却没有任何结果。
我正在创建一个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;
}

但是当触摸时,搜索栏仍然会消失。我还能尝试什么?

我认为我的代码与其他帖子的区别在于我使用了通过编程方式创建的UIViewController而没有使用XIB。有一个UITableView子视图,但它不直接与搜索栏交互。没有UINavigationBar,搜索栏位于视图顶部。

更新:

这是相对于搜索栏的我的视图层次结构:

- UIViewController 
 - UIView
  - UIScrollView
   - UIViewController
    - UIView
     - UISearchBar
3个回答

1

我曾经也遇到过完全相同的问题。解决方法是给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
}

1
我建议有几件事情。
  1. in 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。

对我来说不起作用,但也许是由于我的视图层次结构中的某些问题。我已经更新了带有此信息的问题。 - Cbas
啊,是的,从一开始就知道这个信息会很有帮助。存在一个错误,即 UISearchController 无法正确处理呈现 UISearchController 的视图控制器的视图与其父视图之间存在缩进的情况。这可能是你面临的问题的一部分。 - MDB983

0

嘿,如果你还在寻找解决方案,只需创建一个包装器视图并将你的搜索栏子视图化:

UIView *searchWrapperView = [[UIView alloc] initWithFrame:(CGRect){0, 0, screenWidth, 44}];
[self.view addSubview:searchWrapperView];
[searchWrapperView addSubview:self.searchController.searchBar];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接