[英]Hide/Show back button on Navigation Bar when Search Bar opens/closes
So I have a navigation bar that has a "Back" button on it and a UISearchBar to the right of this: 所以我有一个导航栏,上面有一个“后退”按钮,右边有一个UISearchBar:
![enter image description here][1] ![在此处输入图片描述] [1]
When the UISearchBar opens/shows, the cancel button hides/shows: 当UISearchBar打开/显示时,取消按钮将隐藏/显示:
![enter image description here][2] ![在此处输入图片描述] [2]
What I want is when the UISearchBar opens, I want it to basically "cover" the back button. 我想要的是当UISearchBar打开时,我希望它基本上“覆盖”后退按钮。 When it closes, I want it so "uncover" the back button. 当它关闭时,我希望它能“发现”后退按钮。 Here is my code so far: 到目前为止,这是我的代码:
#import "SearchViewController.h"
@interface SearchViewController ()
@end
- (void)viewDidLoad {
[super viewDidLoad];
if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
UISearchBar *searchBar = [UISearchBar new];
searchBar.showsCancelButton = NO;
[searchBar sizeToFit];
searchBar.delegate = self;
self.navigationItem.titleView = searchBar;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES animated:YES];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:NO animated:YES];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
[searchBar setShowsCancelButton:NO animated:YES];
}
@end
What I've tried was to do: self.navigationItem.hidesBackButton = NO/YES;
我试图做的是: self.navigationItem.hidesBackButton = NO/YES;
in the searchBarTextDidBeginEditing/searchBarTextDidEndEditing but this leaves the spot where the back button was empty: 在searchBarTextDidBeginEditing / searchBarTextDidEndEditing中,但这留有后退按钮为空的位置:
![enter image description here][3] ![在此处输入图片描述] [3]
Is there a way I can have the search bar extend over the back button? 有什么方法可以让搜索栏延伸到后退按钮上吗? Thanks! 谢谢!
Try to do it by 尝试通过
self.navigationItem.leftBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;
or 要么
self.navigationItem.backBarButtonItem=nil;
Updated code : 更新的代码:
@interface SearchViewController ()
{
UIBarButtonItem *backButtonItem;
UIBarButtonItem *negativeSpacer;
}
@end
@implementation SearchViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UISearchBar *searchBar = [UISearchBar new];
searchBar.showsCancelButton = NO;
[searchBar sizeToFit];
searchBar.delegate = self;
self.navigationItem.titleView = searchBar;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.backBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 70.0f, 21.0f)];
UIImage *backImage = [UIImage imageNamed:@"Back.png"];
[backButton setImage:backImage forState:UIControlStateNormal];
[backButton setTitleEdgeInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 0.0)];
[backButton setTitle:@"Back" forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[negativeSpacer setWidth:-15];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,backButtonItem,nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES animated:YES];
self.navigationItem.leftBarButtonItems = nil;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:NO animated:YES];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
[searchBar setShowsCancelButton:NO animated:YES];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,backButtonItem,nil];
}
- (IBAction)backButtonPressed:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.