2016-05-17 108 views
0

我想问一下当我点击IOS 7中的搜索图标时,如何在导航栏中显示搜索栏,几乎所有我浏览的教程都不支持IOS 7,也许你可以给出一个关于如何实现这些功能的想法。在导航栏中显示搜索栏IOS7

这是我们的UI截图:

enter image description here

回答

-1

(1)在其宽度等于它的父视图的视图的顶部在你的XIB添加UISearchBar。添加所需的约束。

enter image description here

(2)设置IBOutlet,并指定委托给搜索栏。

(3)实现以下的委托方法:

# pragma mark 
# pragma mark - SearchBar delegate 

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar 
{ 
    [searchBarWatches resignFirstResponder]; 
    searchBarWatches.text = @""; 

    [UIView animateWithDuration:0.25 animations:^{ 
     searchBarWatches.alpha = 0.0; 
    } completion:^(BOOL finished) { 
     [searchBarWatches setHidden:TRUE]; 
     searchBarWatches.alpha = 1.0; 
    }];  
} 

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar 
{ 
    // searchBarTextDidBeginEditing 
} 

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar 
{ 
    strSearchText = @""; 
    [self searchWatchWithString:strSearchText]; 
} 

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
    searchText = [searchText stringByTrimmingCharactersInSet: 
        [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

    if([searchText isEqualToString:strSearchText]) { 
     return; 
    } 

    if([searchText length] > 0) { 
     strSearchText = searchText; 
    } else { 
     strSearchText = @""; 
    } 

    [self searchWatchWithString:strSearchText]; 
} 

-(void)searchWatchWithString:(NSString *)strSearch 
{ 
    if([strSearch length] > 0) 
    { 
     NSPredicate *predicate1 = [NSPredicate predicateWithFormat:....]; 
     NSPredicate *predicate2 = [NSPredicate predicateWithFormat:....]; 
     NSPredicate *predicate3 = [NSPredicate predicateWithFormat:....]; 
     NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[predicate1, predicate2, predicate3]]; 
     [[self.fetchedResultsController fetchRequest] setPredicate:predicate]; 
    } 

    else 
    { 
     // Empty string 
    } 

    [self performFetch]; 
    [self reloadTableViewAnimated:TRUE]; 
} 

-(void)reloadTableViewAnimated:(BOOL)animated 
{ 
    if(animated) { 
     [tblProducts reloadSections:[NSIndexSet indexSetWithIndex:0]]; 
    } else { 
     [tblProducts reloadData]; 
    } 
} 

(4)搜索按钮,显示搜索栏点击:

- (IBAction)btnSearchPressed:(id)sender 
{ 
    [searchBarWatches setHidden:FALSE]; 
    [self.view bringSubviewToFront:searchBarWatches]; 
    [searchBarWatches becomeFirstResponder]; 
}