我将搜索栏放在导航栏中,其中包含以下代码:
UISearchBar* searchBar = [[UISearchBar alloc] init];
searchBar.delegate = self;
[searchBar setShowsSearchResultsButton:YES];
self.navigationItem.titleView = searchBar;
当我在iOS 7中运行相同的代码时,它可以正常工作。
但搜索栏无法在iOS 6中运行:当我触摸它时,键盘不会出现;当我单击搜索栏中的结果列表按钮时,也没有任何反应。 救命啊!
(基础SDK是iOS 7.0)
解决这样的问题: 只需覆盖导航栏上的搜索栏即可。
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
// Left Cancel button.
UIBarButtonItem *barbtnCancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancel:)];
self.navigationItem.leftBarButtonItem = barbtnCancel;
// Right Search button.
UIBarButtonItem *barbtnSearch = [[UIBarButtonItem alloc] initWithTitle:@"Search" style:UIBarButtonItemStyleBordered target:self action:@selector(search:)];
self.navigationItem.rightBarButtonItem = barbtnSearch;
// Navigation bar.
UINavigationBar* navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
[navBar pushNavigationItem:self.navigationItem animated:YES];
[self.view addSubview:navBar];
// Search bar.
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(53, 0, SCREEN_WIDTH-53-53, 44)];
self.searchBar.delegate = self;
[self.searchBar setBarStyle:UIBarStyleDefault];
[self.searchBar setKeyboardType:UIKeyboardTypeDefault];
[self.searchBar setPlaceholder:@"Search Something"];
[self.searchBar setShowsSearchResultsButton:NO];
[self.view addSubview:self.searchBar];
}
答案 0 :(得分:0)
您需要使用非CGRectZero的框架初始化搜索栏。