当第一个字母按下UISearchBar字段iOS7时,UISearchbar消失

时间:2014-01-08 09:13:15

标签: ios ios7 uisearchbar uisearchdisplaycontroller

这是我在视图controller.h中的代码

#import "AppDelegate.h"

@interface SearchRsultsFanSideViewController : UIViewController<UISearchBarDelegate,UISearchDisplayDelegate>
{
}
@property (strong, nonatomic) IBOutlet UISearchBar *searchData;


@property (strong, nonatomic) UISearchDisplayController *controller;
在viewcontroller.m中

- (void)viewDidLoad
 {
   [super viewDidLoad];
  searchResults=[[NSArray alloc]init];
 self.controller = [[UISearchDisplayController alloc]initWithSearchBar:searchData contentsController:self];
    self.controller.searchResultsDataSource = self;
    self.controller.searchResultsDelegate = self;
}

UIsearchbar的委托方法

#pragma Mark - SearchBar Delegate

- (void)filterData
{

searchResults = nil;

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [cd] %@", searchData.text];
NSArray *arrayaaa=[finalArray copy];
NSLog(@"The result Datas==%@",arrayaaa);
searchResults = [[arrayaaa filteredArrayUsingPredicate:predicate] mutableCopy];


}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

[self.searchDisplayController setActive:YES];

[self filterData];

}

2 个答案:

答案 0 :(得分:1)

试试这个

- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller{
    [controller.searchBar setFrame:CGRectMake(44, 0, 320 - 44, 43)];
    [self.searchDisplayController.searchResultsTableView setDelegate:self];
}

答案 1 :(得分:1)

试试这个:

-(void) viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    if (self.searchDisplayController.isActive) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
}