OStack程序员社区-中国程序员成长平台

标题: ios - 搜索栏打开/关闭时隐藏/显示导航栏上的后退按钮 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:07
标题: ios - 搜索栏打开/关闭时隐藏/显示导航栏上的后退按钮

所以我有一个导航栏,上面有一个“返回”按钮,右边有一个 UISearchBar:

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

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

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

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

#import "SearchViewController.h"

@interface SearchViewController ()

@end

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self respondsToSelectorselector(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)searchBarTextDidBeginEditingUISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:YES animated:YES];
}

- (void)searchBarTextDidEndEditingUISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:NO animated:YES];
}

- (void)searchBarCancelButtonClickedUISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    [searchBar setShowsCancelButton:NO animated:YES];
}

@end

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

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

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



Best Answer-推荐答案


尝试这样做

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 actionselector(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)searchBarTextDidBeginEditingUISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:YES animated:YES];
    self.navigationItem.leftBarButtonItems = nil;
}

- (void)searchBarTextDidEndEditingUISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:NO animated:YES];
}

- (void)searchBarCancelButtonClickedUISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    [searchBar setShowsCancelButton:NO animated:YES];
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,backButtonItem,nil];
}
- (IBAction)backButtonPressedid)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

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






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4