【问题标题】:UISearchController - search bar disappears from wrapper on touchUISearchController - 触摸时搜索栏从包装器中消失
【发布时间】:2016-07-27 08:19:42
【问题描述】:

似乎是一个常见的 iOS 错误,但我已经尝试了所有我能找到的解决方案,但没有看到任何结果。

我正在创建一个 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,搜索栏在视图顶部。

更新:

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

- UIViewController 
 - UIView
  - UIScrollView
   - UIViewController
    - UIView
     - UISearchBar

【问题讨论】:

    标签: ios objective-c ios9 uisearchcontroller


    【解决方案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。

    【讨论】:

    • 对我不起作用,但可能是由于我的视图层次结构中的某些原因。我用它更新了问题
    • 啊,是的,从一开始就知道这会很有帮助。存在一个错误,其中 UISearchController 无法正确处理呈现 UISearchController 的视图控制器的视图从其父视图插入的情况。这可能是您面临的问题的一部分。
    【解决方案2】:

    我遇到了同样的问题。为我解决的问题是将锚点添加到 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
    }
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-26
        • 2023-03-09
        • 2016-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多