【问题标题】:how to add search bar under the navigation title如何在导航标题下添加搜索栏
【发布时间】:2016-10-12 08:58:36
【问题描述】:

我需要导航标题下的搜索栏,如何以编程方式或通过故事板进行操作。

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

【问题讨论】:

  • 你能显示一些你需要的屏幕截图吗
  • 您面临什么问题?用屏幕告诉我们
  • 你需要这个在导航栏或导航栏底部
  • 在导航控制器内部。它包含导航标题和搜索栏
  • 但问题是我添加了隐藏搜索栏的导航标题,并且搜索栏添加到导航标题导航标题被隐藏了。

标签: ios objective-c uisearchbar


【解决方案1】:

您已将搜索栏添加为UIBarButtonItem,这就是它不可见的原因。

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

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

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

更新:

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

输出:

更新的输出

自定义文本字段:

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 像素,类似于搜索栏的文本字段。

这是图片:

然后输出:

希望对您有所帮助...

编码愉快...

【讨论】:

  • 效果很好。但我需要为导航控制器和搜索栏设置背景颜色。
  • 您可以使用此代码使导航栏显示为红色:self.navigationController.navigationBar.barTintColor = [UIColor redColor];
  • 此代码仅更改导航控制器的颜色。但我需要在导航控制器内设置搜索,并且在未显示的标题下设置搜索。
  • 我的意思是子视图不是视图。
  • 由于导航控制器是一体的,所以在一个屏幕中改变,改变所有。所以解决方法是在除屏幕外的其他屏幕上将颜色设置为清晰的颜色。记得把设置颜色的代码放在“viewWillAppear”中
【解决方案2】:

我们也可以使用文本框作为搜索栏,它在我的应用中运行良好,

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

现在它可以正常工作了..

【讨论】:

    猜你喜欢
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    相关资源
    最近更新 更多