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

时间:2015-07-04 03:06:41

标签: 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

我尝试过做的事情:{b}在searchBarTextDidBeginEditing / searchBarTextDidEndEditing中,但这会留下后退按钮为空的位置:

![在此输入图片说明] [3]

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

1 个答案:

答案 0 :(得分:4)

尝试通过

进行
[ui]
username = My Name <myname@domain.com>

[extensions]
hgext.convert=
hgext.bookmarks =
hggit =

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

更新的代码:

self.navigationItem.backBarButtonItem=nil;