简体   繁体   English

在导航栏IOS7中显示搜索栏

[英]Display Search Bar in Navigation Bar IOS7

I would like to ask on how to display a search bar in navigation bar when I tapped the Search Icon in IOS 7, almost all of the tutorial that I browse is not supporting IOS 7, maybe you can give an idea on how to implement that functionalities. 当我在IOS 7中点击“搜索图标”时,我想问一下如何在导航栏中显示搜索栏,几乎我浏览的所有教程都不支持IOS 7,也许您可​​以对如何实现该想法有所了解功能。

Here's the screenshot of our UI: 这是我们的用户界面的屏幕截图:

在此处输入图片说明

(1) Add UISearchBar in your XIB at the top of the view having width equal to its parent view. (1)在XIB的顶部添加宽度等于其父视图的UISearchBar Add required constraints. 添加所需的约束。

在此处输入图片说明

(2) Set IBOutlet and assign delegate to that search bar. (2)设置IBOutlet并将委托分配给该搜索栏。

(3) Implement following delegate methods: (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) On click of search button, show search bar: (4)单击搜索按钮,显示搜索栏:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM