iOS,迅捷。如何使用下拉列表制作浮动搜索栏? (包括图片) [英] iOS, swift. How to make a floating search bar with drop down list like this?? (Image included)

查看:121
本文介绍了iOS,迅捷。如何使用下拉列表制作浮动搜索栏? (包括图片)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用标准iOS UIKit 吗?

Can I make this with standard iOS UIKit?

当我谷歌搜索 UISearchBar ,所有教程都附带 UITableView 。这不是我想要的。

When I google for UISearchBar, all tutorials come with UITableView. Which is not what I want.

我想要这个搜索栏


  • 漂浮在所有内容之上

  • 显示用户输入时可能匹配选项的下拉列表

推荐答案

要像这样制作搜索栏,你想在导航栏控制器中插入按钮,并将背景图像设置为 search.png (你的图像)。因此,当用户点击此按钮时,目标为搜索栏将被打开。请查看以下代码供您参考。

to make searchbar like this you want to insert Button in Navigation bar controller and set background image as search.png(your image). so, when user click on this button set target as searchbarwill be open. please check below code for your reference.

首先在您的 .h 文件中设置委托方法。

First of all set delegate method in you .h file.

@interface FriendsViewController : UIViewController <UISearchDisplayDelegate,UISearchBarDelegate,UIAlertViewDelegate>
 @property (nonatomic, strong) UIButton *searchButton;
 @property (nonatomic, strong) UIBarButtonItem *searchItem;
 @property (nonatomic, strong) UISearchBar *searchBar;
 @property (strong, nonatomic) UISearchController *searchController;
 @property (strong, nonatomic) UISearchDisplayController *d1;

然后在你的nevigationbar中插入按钮。

then Insert button in your nevigationbar.

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(279,9,25, 25)];
[btn setImage:[UIImage imageNamed:@"search"] //put here your searchimage
forState:UIControlStateNormal];
[btn setTitle:@"" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(clickme:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barbtn=[[UIBarButtonItem alloc]initWithCustomView:btn];
self.tabBarController.navigationItem.rightBarButtonItem=barbtn;
[self.tabBarController.navigationController.navigationBar setHidden:NO];

现在你必须在 clickme上设置searchcontroller 你的按钮方法。

Now you have to set searchcontroller on clickme your button method.

- (IBAction)clickme:(id)sender{
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 300, 44.0)];
searchBar.autoresizingMask =0;
searchBar.delegate = self;
searchBar.placeholder = @"Search for items...";
searchBar.showsScopeBar=YES;

UIView *searchBarWrapper = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
searchBarWrapper.autoresizingMask = 0;
[searchBarWrapper addSubview:searchBar];

self.searchItem = [[UIBarButtonItem alloc] initWithCustomView:searchBarWrapper];
self.tabBarController.navigationItem.leftBarButtonItem = self.searchItem;

self.navigationItem.rightBarButtonItem = nil;
self.navigationItem.titleView = nil;

  ////////////// ~ Search Display Controller as Object ~/////////////////////////////

self.d1 = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
self.d1.delegate = self;
self.d1.searchResultsDataSource = self;
self.d1.searchResultsDelegate = self;
self.d1.searchResultsTableView.rowHeight = 40;
self.d1.displaysSearchBarInNavigationBar = YES;
self.searchBar.translucent = NO;
self.searchBar.barTintColor = [UIColor grayColor];
self.d1.searchBar.tintColor = [UIColor blueColor];

[searchBar sizeToFit];

}

这篇关于iOS,迅捷。如何使用下拉列表制作浮动搜索栏? (包括图片)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆