2

Поэтому у меня есть панель навигации, которая имеет кнопку «Назад» на нем и UISearchBar справа от этого:Скрыть/Показать кнопку назад на панели навигации, когда Search Bar открывается/закрывается

[введите описание изображения здесь] [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]

Есть ли способ, которым строка поиска может быть продолжена над кнопкой «Назад»? Благодаря!

ответ

4

Попробуйте сделать это

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

Это не помогло моей ситуации. Тем не менее, я подошел поближе, но когда я нажму кнопку «Назад», стрелка отсутствует. Можете ли вы мне помочь? Я обновил свой вопрос – user1282637

+0

спасибо, я постараюсь это скоро – user1282637