2016-10-12 73 views
0

please see my screen shot i need thisthis 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"; 
+0

您可以显示一些屏幕截图您是如何需要 –

+0

什么问题你脸?告诉我们一些屏幕 –

+0

你需要这里面的导航控制器内的导航栏 –

回答

2

您已将搜索栏添加为UIBarButtonItem,这就是为什么它不可见。

如果你想到位导航栏使用此代码

代码的标题视图来显示搜索栏,以显示导航栏代替导航的标题:

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

输出:

enter image description here

守则显示导航栏导航栏下面:

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]; 

UPDATE:

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]}]; 

输出:

enter image description here

更新输出

要定制文本字段:

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]; 

[searchBar1 setSearchFieldBackgroundImage:[UIImage imageNamed:@"text-input.png"] forState:UIControlStateNormal]; 

我已经使用的图像是一样的搜索栏的文本字段高度30像素。

这里是图像:

enter image description here

输出:

enter image description here 希望它可以帮助...

快乐编码...

updated output

+0

是行之有效的。但我需要设置导航控制器和搜索栏的背景颜色如何做到这一点。 – ios

+0

您可以使用此代码使导航栏显示为红色: self.navigationController.navigationBar.barTintColor = [UIColor redColor]; – Janmenjaya

+0

此代码仅更改导航控制器的颜色。但我需要在导航控制器中设置搜索,并且在标题不可见的情况下进行搜索。 – ios

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]; 
 
    } 
 
}

现在,它会正常工作..

相关问题