2013-10-08 64 views
6

搜索欄中的取消按鈕在iOS 7中不起作用,當搜索欄初始隱藏時。搜索欄取消按鈕有時不在ios 7中工作

我按照這個教程創建tableview中搜索欄:

raywenderlich tutorial

有在本教程中示例項目,爲更好地利用這個項目比我的解釋:)

在iOS系統5和6工作正常。 我已經審查過所有代表。

有兩種可能性。第一種是在酒吧隱藏時按下按鈕,第二種是在顯示酒吧時按下按鈕(用手勢移動桌子可以看到搜索欄)

如果搜索欄最初是隱藏的取消按鈕不工作,它不叫calcel委託方法:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 

對不起,我不能解釋得更好。

感謝

+0

我有同樣的問題。 [self.searchDisplayController.searchBar becomeFirstResponder];僅當搜索欄已經可見時才起作用。 –

+0

是的,因爲我的搜索欄現在總是可見的。 – mjimcua

回答

1

把這個代碼在您的項目,將工作我已經測試它是否正常工作

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchbar 
{ 
[searchbar resignFirstResponder]; 

for (UIView *possibleButton in searchbar.subviews) 
{ 
    if ([possibleButton isKindOfClass:[UIButton class]]) 
    { 
     UIButton *cancelButton = (UIButton*)possibleButton; 
     cancelButton.enabled = YES; 
     break; 
    } 
} 

} 
3

我GOOGLE了遍佈互聯網,但沒有找到一個解決方案。所以我改變了UItableview的行爲。

而不是[searchBar becomeFirstResponder];我向下滾動tableview。

- (IBAction)goToSearch:(id)sender { 

scroll down to show the table. 
// CGRect newBounds = self.tableView.bounds; 
// newBounds.origin.y =0; 
//  
// self.tableView.bounds = newBounds; 
//[searchBar becomeFirstResponder]; 

    CGPoint contentOffset=self.tableView.contentOffset; 
    contentOffset.y=0; 
    [self.tableView setContentOffset:contentOffset animated:YES]; 


} 

在我viewDidLoad中:

//  CGRect newBounds = self.tableView.bounds; 
//  newBounds.origin.y = newBounds.origin.y + searchBar.bounds.size.height; 
     // self.tableView.bounds = newBounds; 

     CGPoint contentOffset=self.tableView.contentOffset; 
     contentOffset.y=self.tableView.bounds.origin.y + searchBar.bounds.size.height; 
     self.tableView.contentOffset=contentOffset; 

如果發現某些原因,在iOS的7,變動表視圖界引起搜索欄中消失。 希望有所幫助。

+0

我使用您的更改並且不起作用,謝謝! – mjimcua

+0

它適合我。也許你可以在問題中發佈一些代碼? – Kiddo

+0

這是一個奇怪的解決方法,但這個工程,謝謝! – PaperThick

0

此問題似乎來自導航欄中半透明屬性的新行爲。

由於默認iOS 7導航欄是半透明的。它看起來像是在按下按鈕後顯示它時與搜索欄重疊。

嘗試在你的控制器設置:

float osVersion = [[[UIDevice currentDevice] systemVersion] floatValue]; 
if (osVersion >= 7.0) 
{ 
    self.navigationController.navigationBar.translucent = NO; 
} 

這應該迅速解決問題。

但我認爲,爲了更好地解決問題,您應該看到iOS 7 transition guide,他們解釋瞭如何處理半透明導航欄。

希望有所幫助。

1

對我說,代碼工作的iOS7:

- (IBAction)goToSearch:(id)sender { 
    [self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO]; 
    [candySearchBar becomeFirstResponder]; 
} 
+0

我不知道爲什麼這個工作,但它確實。 –

0

我假設你已經設置_searchBar.delegate = self並在類中實現UISearchBarDelegate

這是你如何做到這一點:

- (void) searchBarTextDidBeginEditing:(UISearchBar *)searchBar{ 
    [searchBar setShowsCancelButton:YES animated:YES]; 
} 

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{      // called when cancel button pressed 
    searchBar.text = nil; 

    //hide cancel button 
    [_searchBar setShowsCancelButton:NO animated:YES]; 

    [searchBar resignFirstResponder]; 
} 
相關問題