2013-10-03 63 views
7

我有一个xCode项目,其中包含一个带有“搜索栏和搜索显示控制器”的tableview,允许用户优化显示项目的列表。一般而言,遵循http://www.raywenderlich.com/16873/how-to-add-search-into-a-table-view中提供的指导。我最近下载了支持iOS 7的最新xCode(版本5.0(5A1413)),并已针对不同目标测试了相关应用。UISearchBar的取消和清除按钮不在iOS 7中工作

在iOS 6目标(模拟器或实际设备)上运行此应用程序时,它按预期工作,这意味着按取消按钮删除搜索栏并按清除按钮(灰色小x)将清除所有搜索条件已由用户键入。但是当项目在iOS 7目标上运行时,清除和取消按钮都不起作用。

的searchBarCancelButtonClicked方法在这个项目中实现了,我已经验证了它不叫目标运行iOS 7时

- (void)searchBarCancelButtonClicked:(UISearchBar *)SearchBar 
{ 
    NSLog(@"searchBarCancelButtonClicked called"); 

    self.searchBar.text = nil; 

    … 

    // Hide Search bar when cancelled 
    [self hideSeachBar]; 

    [self.searchBar resignFirstResponder]; 

    … 
    } 

我的表视图控制器设置是UISearchDisplayDelegate和UISearchBarDelegate。看起来,这仍然是作为searchBar:textDidChange:在iOS 6或7目标上调用。

@interface ItemViewController() <UISearchDisplayDelegate, UISearchBarDelegate> 
… 
@end 

我看不到与此相关的,或者提到需要做支持iOS7任何重新编码在任何iOS 7变材料(如https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TransitionGuide/Bars.html#//apple_ref/doc/uid/TP40013174-CH8-SW1)任何其他职位。

对此有何看法?谢谢

+0

我还没有机会查看答案,但我也只是注意到同一问题的这些帖子:https://devforums.apple.com/message/880210#880210和https://devforums.apple。 COM /消息/ 876407#876407。 – Mike

+0

该代码适用于iOS7:http://stackoverflow.com/a/22287788/771689 –

回答

3

我有同样的问题,我试着用下面的代码。请试试这个。

-(void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller 
{ 
    controller.active = YES; 

    [self.view addSubview:controller.searchBar]; 
    [self.view bringSubviewToFront:controller.searchBar]; 
} 

- (void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView { 

    tableView.frame = self.archiveListTblView.frame; 
} 

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller 
{ 
    controller.active=NO; 

    [self.view addSubview:controller.searchBar]; 
} 
+0

好奇你的self.archiveListTblView是如何设置和使用的。 – Mike

+0

Hello Mike,以我的观点来看:iOS7会自动为其诞生窗口进行布局。这就是我们面临的问题的原因。还有一件事,我尝试过使用“self.view.frame”而不是“self.archiveListTblView.frame”。即使这个代码也适用于我。所以,问题是我们没有为tableview设置框架。 :-P – Prabakaran

0

我在互联网上搜索了一遍,找不到解决方案。所以我改变了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,变动表视图界引起搜索栏中消失。 希望有所帮助。

1

按钮取消和按钮清除将不起作用,如果你触摸按钮搜索导航。如果你点击搜索栏来搜索星号,它会正常工作 =>这意味着如果搜索栏在屏幕上不可见,然后开始搜索,那么这些按钮看起来也是禁用的。

因此,我找到了一个解决方案,但它并不完全完美。 在使搜索栏成为第一个响应之前,您必须将表视图滚动到顶部。试试这个代码。

-(IBAction)goToSearch:(id)sender { 
    // Make search bar visible on screen before make it response 
    [_tableView setContentOffset:CGPointMake(0, 0) animated:NO]; 

    // Make search bar active 
    [candySearchBar becomeFirstResponder]; 
} 

唯一的问题出现,如果你这样做,你的表视图会滚动到顶部,这意味着当你取消搜索,你以前失去当前单元格的索引。

1

此问题似乎来自导航栏中半透明属性的新行为。

由于默认iOS 7导航栏是半透明的。它看起来像是在按下按钮后显示它时与搜索栏重叠。如果您滚动到listView的顶部并使用搜索栏,它应该正常工作。

尝试在你的控制器设置:

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

这应该迅速解决问题。

但我认为,为了更好地解决问题,您应该看到iOS 7 transition guide,他们解释了如何处理半透明导航栏。

希望有所帮助。

1

我也有这个问题。奇怪的是UISearchBarDelegate的其他代表方法正在被调用。一种解决方法可能是:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ 
    if ([searchText length] == 0) { 
     NSLog("No Text"); 
    } 
} 

它为我

0

就我而言,我已经重新定位,搜索栏在屏幕的顶部,并有其重叠在搜索栏一种无形的视图。

因此实际上没有触及取消按钮。

所以,我在下面调用[searchBarTextDidBeginEditing]方法时带了seachbar front。

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{ 
    lc_SearchBarYPos.constant = 20.0f; //this is code to reposition the searchbar 
    [self.view bringSubviewToFront:searchBar]; 
} 

希望这可能有所帮助。

相关问题