如何用 UISearchBar 替换导航栏?
How to replace the navigation bar with an UISearchBar?
我想在显示后用搜索栏替换导航栏。
或
如果导航栏总是覆盖我的主视图控制器的内容,我会很好。
我这样设置搜索控制器:
_searchController = [[UISearchController alloc] initWithSearchResultsController:[[MySearchViewController alloc] init]];
_searchController.delegate = self;
_searchController.obscuresBackgroundDuringPresentation = TRUE;
_searchController.hidesNavigationBarDuringPresentation = TRUE;
_searchController.searchBar.placeholder = @"Search...";
_searchController.searchBar.delegate = self;
我显示这样的搜索控制器:
- (void)searchButtonTapped:(id)sender
{
self.navigationItem.searchController = _searchController;
[self.navigationController.view setNeedsLayout];
[self.navigationController.view layoutIfNeeded];
[self.navigationItem.searchController.searchBar becomeFirstResponder];
}
我隐藏这样的搜索控制器:
- (void)willDismissSearchController:(UISearchController*)searchController
{
self.navigationItem.searchController = nil;
[self.navigationController.view setNeedsLayout];
}
我知道有一个 属性 searchController.hidesNavigationBarDuringPresentation
接近我想要的但完全正确。因为导航栏只会在我进入搜索栏时隐藏,但我希望它在我 显示 搜索栏时就已经隐藏了。
只要我按下导航栏中的搜索图标按钮,它就会被搜索控制器替换,并且只有搜索栏在顶部可见。有谁知道如何做到这一点?
我已经试过了:
- 当我用
[self.navigationController setNavigationBarHidden:YES animated:NO];
显示搜索控制器时隐藏导航栏
- 控制搜索栏高度限制并根据可见性更改它
- 将搜索控制器设置为
navigationItem.titleView
-> 这不是我想要的
- 根据可见性隐藏搜索控制器 -> 导航栏保持相同高度仅隐藏搜索控制器
- 使用
self.definesPresentationContext
但这似乎与我需要的设置不同
- 查看了
self.navigationItem.hidesSearchBarWhenScrolling
但这不适用,因为我不使用 table 视图控制器
我可以做出第一个选择
I would like to replace the navigation bar with the search bar once shown and back.
工作。
当显示 UISearchBar 时,我使用 显示和隐藏 左右蝙蝠按钮项目 的方法.
如果有更简单的方法来实现同样的目标,我会很感兴趣吗?
@interface MyViewController() <UISearchControllerDelegate, SearchDelegate>
@end
@implementation MyViewController
{
UIView* _navigationItemTitleView;
NSArray<UIBarButtonItem*>* _leftBarButtonItems;
NSArray<UIBarButtonItem*>* _rightBarButtonItems;
}
- (void)viewDidLoad
{
_searchController = [[UISearchController alloc] initWithSearchResultsController:[[MySearchViewController alloc] init]];
// ....
_navigationItemTitleView = self.navigationItem.titleView;
_leftBarButtonItems = self.navigationItem.leftBarButtonItems;
_rightBarButtonItems = self.navigationItem.rightBarButtonItems;
}
- (void)searchButtonTapped:(id)sender
{
// show the search bar in the navigation item
self.navigationItem.leftBarButtonItems = nil;
self.navigationItem.rightBarButtonItems = nil;
self.navigationItem.titleView = _searchController.searchBar;
_searchController.active = TRUE;
}
- (void)willDismissSearchController:(UISearchController*)searchController
{
// hide the search bar and restore previous state of the navigation item
self.navigationItem.leftBarButtonItems = _leftBarButtonItems;
self.navigationItem.rightBarButtonItems = _rightBarButtonItems;
self.navigationItem.titleView = _navigationItemTitleView;
}
我想在显示后用搜索栏替换导航栏。
或
如果导航栏总是覆盖我的主视图控制器的内容,我会很好。
我这样设置搜索控制器:
_searchController = [[UISearchController alloc] initWithSearchResultsController:[[MySearchViewController alloc] init]];
_searchController.delegate = self;
_searchController.obscuresBackgroundDuringPresentation = TRUE;
_searchController.hidesNavigationBarDuringPresentation = TRUE;
_searchController.searchBar.placeholder = @"Search...";
_searchController.searchBar.delegate = self;
我显示这样的搜索控制器:
- (void)searchButtonTapped:(id)sender
{
self.navigationItem.searchController = _searchController;
[self.navigationController.view setNeedsLayout];
[self.navigationController.view layoutIfNeeded];
[self.navigationItem.searchController.searchBar becomeFirstResponder];
}
我隐藏这样的搜索控制器:
- (void)willDismissSearchController:(UISearchController*)searchController
{
self.navigationItem.searchController = nil;
[self.navigationController.view setNeedsLayout];
}
我知道有一个 属性 searchController.hidesNavigationBarDuringPresentation
接近我想要的但完全正确。因为导航栏只会在我进入搜索栏时隐藏,但我希望它在我 显示 搜索栏时就已经隐藏了。
只要我按下导航栏中的搜索图标按钮,它就会被搜索控制器替换,并且只有搜索栏在顶部可见。有谁知道如何做到这一点?
我已经试过了:
- 当我用
[self.navigationController setNavigationBarHidden:YES animated:NO];
显示搜索控制器时隐藏导航栏
- 控制搜索栏高度限制并根据可见性更改它
- 将搜索控制器设置为
navigationItem.titleView
-> 这不是我想要的 - 根据可见性隐藏搜索控制器 -> 导航栏保持相同高度仅隐藏搜索控制器
- 使用
self.definesPresentationContext
但这似乎与我需要的设置不同 - 查看了
self.navigationItem.hidesSearchBarWhenScrolling
但这不适用,因为我不使用 table 视图控制器
我可以做出第一个选择
I would like to replace the navigation bar with the search bar once shown and back.
工作。
当显示 UISearchBar 时,我使用 显示和隐藏 左右蝙蝠按钮项目 的方法.
如果有更简单的方法来实现同样的目标,我会很感兴趣吗?
@interface MyViewController() <UISearchControllerDelegate, SearchDelegate>
@end
@implementation MyViewController
{
UIView* _navigationItemTitleView;
NSArray<UIBarButtonItem*>* _leftBarButtonItems;
NSArray<UIBarButtonItem*>* _rightBarButtonItems;
}
- (void)viewDidLoad
{
_searchController = [[UISearchController alloc] initWithSearchResultsController:[[MySearchViewController alloc] init]];
// ....
_navigationItemTitleView = self.navigationItem.titleView;
_leftBarButtonItems = self.navigationItem.leftBarButtonItems;
_rightBarButtonItems = self.navigationItem.rightBarButtonItems;
}
- (void)searchButtonTapped:(id)sender
{
// show the search bar in the navigation item
self.navigationItem.leftBarButtonItems = nil;
self.navigationItem.rightBarButtonItems = nil;
self.navigationItem.titleView = _searchController.searchBar;
_searchController.active = TRUE;
}
- (void)willDismissSearchController:(UISearchController*)searchController
{
// hide the search bar and restore previous state of the navigation item
self.navigationItem.leftBarButtonItems = _leftBarButtonItems;
self.navigationItem.rightBarButtonItems = _rightBarButtonItems;
self.navigationItem.titleView = _navigationItemTitleView;
}