ios - 当搜索栏打开/关闭时隐藏/显示导航栏上的后退按钮

标签 ios objective-c iphone uinavigationcontroller uisearchbar

所以我有一个导航栏,上面有一个“后退”按钮,右侧有一个 UISearchBar:

![在此处输入图像描述][1]

当 UISearchBar 打开/显示时,取消按钮隐藏/显示:

![在此处输入图像描述][2]

我想要的是当 UISearchBar 打开时,我希望它基本上“覆盖”后退按钮。当它关闭时,我希望它“露出”后退按钮。这是到目前为止我的代码:

#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

我尝试做的是: self.navigationItem.hidesBackButton = NO/YES; 在 searchBarTextDidBeginEditing/searchBarTextDidEndEditing 中,但这留下了后退按钮为空的位置:

![在此处输入图像描述][3]

有没有办法让搜索栏延伸到后退按钮上?谢谢!

最佳答案

尝试这样做

self.navigationItem.leftBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;

self.navigationItem.backBarButtonItem=nil;

更新的代码:

@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];
}

关于ios - 当搜索栏打开/关闭时隐藏/显示导航栏上的后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31216483/

相关文章:

ios - NSURLConnection 在两次后崩溃

objective-c - NSSlider NSSliderCell 裁剪自定义旋钮

iphone - 针对 iPhone iPad 的 CSS 样式表不起作用

ios - 递增 NSDate 用于 for 循环

ios - 使用 SceneDelegate 在 AppDelegate 中设置 UIWindow

java - 在 Java/Objective C 中,类/实例对象实际上如何从内存的 Text/Method Segment 访问方法?

ios - UITableView 单元格内容未正确显示

iphone - iOS 6 问题将 MPMediaItem 转换为 NSData

iphone - 如何获取从 iPhone 库中选择的多张图片的路径?

ios - 有什么方法可以将 MKPlacemark 实例保存到 Realm?