如何在导航标题下添加搜索栏

时间:2016-10-12 08:58:37

标签: ios objective-c uisearchbar

please see my screen shot i need this this is the previous output before adding image我需要导航标题下的搜索栏如何以编程方式或通过故事板进行操作。

UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,30, 250,44)];
searchBar.placeholder = @"Search";
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc]initWithCustomView:searchBar];
searchBarItem.tag = 123;
searchBarItem.customView.hidden = YES;
searchBarItem.customView.alpha = 0.0f;
self.navigationItem.leftBarButtonItem = searchBarItem;
self.navigationItem.title = @"locations";

2 个答案:

答案 0 :(得分:2)

您已将搜索栏添加为using std::experimental::make_array; myVector.push_back(std::make_pair(Key, make_array(Pair1, Pair2, Pair3) )); ,这就是为什么它不可见。

如果要显示搜索栏以代替导航栏的标题视图,请使用此代码

显示导航栏代替导航标题的代码:

UIBarButtonItem

输出:

LIVE from GCC

显示导航栏下方导航栏的代码:

UISearchBar *searchBar = [[UISearchBar alloc]init];
searchBar.tintColor = [UIColor grayColor];
searchBar.backgroundColor = [UIColor redColor];
searchBar.placeholder = @"Search";
self.navigationItem.titleView = searchBar;

更新:

UISearchBar *searchBar1 = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 64)];
searchBar1.tintColor = [UIColor grayColor];
searchBar1.backgroundColor = [UIColor redColor];
searchBar1.placeholder = @"Search";
[self.view addSubview:searchBar1];

输出:

enter image description here

更新了输出

自定义textField:

self.navigationController.navigationBar.barTintColor = [UIColor redColor];
searchBar1.searchBarStyle = UISearchBarStyleMinimal;

UISearchBar *searchBar1 = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 64)];
searchBar1.tintColor = [UIColor grayColor];
searchBar1.searchBarStyle = UISearchBarStyleMinimal;
searchBar1.backgroundColor = [UIColor redColor];
searchBar1.placeholder = @"Search";
[self.view addSubview:searchBar1];

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

我使用的图像高度为30像素,就像搜索栏的文本字段一样。

这是图像:

enter image description here

输出:

enter image description here 希望它有所帮助...

快乐的编码......

enter image description here

答案 1 :(得分:0)

我们也可以使用文本框作为搜索栏,它在我的应用程序中运行正常,

转到StoryBoard并执行以下更改

从对象Library--> type view并拖动到storyboard并粘贴到导航栏下方。

将文本框添加到视图中并分配出口和代理。

现在转到viewcontroller.m并粘贴以下代码



-(void)textFieldDidChange:(UITextField *)textField
{
    searchTextString=textField.text;
    [self updateSearchArray:searchTextString];
}


-(void)updateSearchArray:(NSString *)searchText
{
    if(searchText.length==0)
    {
        isFilter=NO;
    }
    else
    {
        isFilter=YES;
        searchArray=[[NSMutableArray alloc]init];
    
        for(NSString *string in responceArray)
        {
            NSRange stringRange = [[NSString stringWithFormat:@"%@",string]
                               rangeOfString:searchtext.text options:NSCaseInsensitiveSearch];
        
            if (stringRange.location != NSNotFound)
            {
                [searchArray addObject:string];
            }
        }
        [jobsTable reloadData];
     }
}




现在它会正常工作..