似乎是一个常见的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
答案 0 :(得分:1)
我会建议一些事情。
// 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。
答案 1 :(得分: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
}
答案 2 :(得分:0)
嘿,如果您仍在寻找解决方案,只需创建一个包装视图并对其子栏进行子查看:
UIView *searchWrapperView = [[UIView alloc] initWithFrame:(CGRect){0, 0, screenWidth, 44}];
[self.view addSubview:searchWrapperView];
[searchWrapperView addSubview:self.searchController.searchBar];