[英]iOS 7 doesn't show cancel button of search bar in navigation bar
在應該在iOS 6和iOS 7上運行的應用程序中,如果應用程序在iOS 7上運行,導航欄中嵌入的搜索欄的取消按鈕將不再顯示。在iOS 6上,它可以正常工作。
搜索欄位於導航欄的標題視圖中,如果搜索欄成為第一個響應者,則應顯示取消按鈕:
IOS 7
iOS 6
在一個獨立的測試用例中,代碼非常簡單:
@interface MyViewController : UITableViewController<UISearchBarDelegate>
@property (nonatomic) IBOutlet UISearchBar* searchBar;
@end
@implementation MyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.titleView = self.searchBar;
}
- (void) searchBarTextDidBeginEditing: (UISearchBar*) searchBar {
[searchBar setShowsCancelButton: YES animated: YES];
}
@end
這是我在文檔中遺漏的iOS 7中的故意更改嗎? 如果是的話,應該是什么選擇?
如果沒有,我的代碼中是否犯了錯誤?
看起來你正確地做了一切,但顯然Apple已經改變了iOS 7中的一些東西。 根據 iOS 7 中的這個問題 ,取消按鈕不會出現在UINavigationBar
嵌入的UISearchBar
。
根據開發人員文檔 , showsCancelButton
屬性可能與setShowsCancelButton:Animated
方法略有不同。 試着這樣做:
searchBar.showsCancelButton = YES;
[searchBar setShowsCancelButton:YES animated:YES];
我不確定這是否會產生任何影響。 您還可以嘗試將代碼放在不同的委托方法中:
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar; // return NO to not become first responder
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar; // called when text starts editing
您可能還想查看iOS 7更改日志 。 看起來Apple在添加到UINavigationBar
時改變了行為或UISearchDisplayController
/ UISearchBar
。 看看UIKit部分下的最后一個要點(雖然不清楚究竟發生了什么變化)。
您可能還想嘗試使用UISerachDisplayController
。 更簡單的是將UISearchBar
嵌入UITableView
的標題中。
我只是通過添加rightBarButtonItem來解決這個問題很簡單:)
self.navigationItem.titleView = self.searchBar;
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Cancel", nil) style:UIBarButtonItemStylePlain target:self action:@selector(didClickCancelButton:)] autorelease];
但是你必須檢查當前的iOS版本是否> = 7.0,否則你會得到兩個“取消”按鈕..
BTW此方法允許您使用“取消”按鈕,該按鈕始終啟用
iOS 7與導航欄中的iOS 6不同,因此如果要在導航欄中顯示UISearch欄,可以嘗試以下操作:
將你的UISearchbar放在像這樣的UIView [self.searchView addSubview self.searchBar]
,並將導航欄的titleView設置為你的searchView,如self.navagitioncontroller.navigationItem.titleView = self.searchView
希望這對你有用
如果您將UISearchBar與UISearchDisplayController一起使用,則可以在“searchDisplayControllerWillBeginSearch”委托方法中設置取消按鈕以顯示,如下所示:(iOS 7已測試)
-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
controller.searchBar.showsCancelButton = YES;
}
iOS6和iOS7之間似乎有一個變化,因為xxxDidYYY
方法對UI的更改有時不起作用,您必須在xxxWillYYY
方法或從主事件循環執行的某些代碼中執行此操作(例如,在阻止或短暫延遲后)。
在你的情況下,試試這個:
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = YES;
return YES;
}
在我看來這是一個錯誤。 繼承我的解決方法。 它並不完美,但它適用於iOS 6和7。 在iOS7上,搜索欄文本字段在取消按鈕時滑過,而在iOS6上,文本字段寬度擴展沒有動畫。
@interface FTViewController ()
@property(nonatomic, strong) UISearchBar *searchBar;
@end
@implementation FTViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.searchBar = [[UISearchBar alloc] init];
self.searchBar.delegate = self;
if (NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_6_1) {
// iOS 6.1 and older (only tested on 6.1)
[self.searchBar sizeToFit];
self.searchBar.backgroundImage = nil;
}
self.navigationItem.titleView = self.searchBar;
}
-(void)cancelBarButtonItemClicked:(id)sender
{
[self searchBarCancelButtonClicked:self.searchBar];
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
[self.navigationItem setRightBarButtonItem:nil animated:YES];
}
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelBarButtonItemClicked:)];
[self.navigationItem setRightBarButtonItem:cancelBtn animated:YES];
return YES;
}
@end
有兩種選擇:
我的偏好是第二個,但它看起來更像是第一個原生的。
這確實是一個從7.1開始修復的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.