2015-07-04 44 views
2

所以我有上有一個「後退」按鈕,導航欄和一個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 

我已經嘗試了做:在searchBarTextDidBeginEditing/searchBarTextDidEndEditing self.navigationItem.hidesBackButton = NO/YES;但離開那個地方後退按鈕是空的!

[輸入圖像描述這裏] [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