【问题标题】:Hide/Show back button on Navigation Bar when Search Bar opens/closes搜索栏打开/关闭时隐藏/显示导航栏上的后退按钮
【发布时间】:2015-09-21 20:47:51
【问题描述】:

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

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

【问题讨论】:

    标签: ios objective-c iphone uinavigationcontroller uisearchbar


    【解决方案1】:

    尝试这样做

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

    【讨论】:

    • 这对我的情况没有帮助。但是,我确实靠近了,但是当我重新显示后退按钮时,箭头不见了。你能帮我解决这个问题吗?我已经更新了我的问题
    猜你喜欢
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多