• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 11:07:50 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

所以我有一个导航栏,上面有一个“返回”按钮,右边有一个 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap