iOS,快。如何使用这样的下拉列表制作 float 搜索栏? (包括图像)

标签 ios xcode swift uisearchbar

我可以用标准的 iOS UIKit 做这个吗?

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

我希望这个搜索栏是

  • 漂浮在一切之上
  • 在用户输入时显示可能匹配选项的下拉列表

enter image description here

最佳答案

要制作这样的搜索栏,您需要在导航栏 Controller 中插入 Button 并将背景图像设置为 search.png(您的图像)。因此,当用户单击此按钮时,会将目标设置为 searchbar 将打开。请检查以下代码以供引用。

首先在你的 .h 文件中设置委托(delegate)方法。

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

然后在您的导航栏中插入按钮。

- (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。

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

}

Barbutton

当你点击搜索图标时

enter image description here

关于iOS,快。如何使用这样的下拉列表制作 float 搜索栏? (包括图像),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30752638/

相关文章:

ios - Xcode 9 _OBJC_CLASS_$_FBSDKApplicationDelegate

iphone - NSUserDefaults - 保存 scrollView 偏移量

ios - 在上传到 Github 时,为了安全起见,我应该从 Xcode 项目中删除任何内容吗?

ios - 为什么 playground 在 getter 结果前加上 "some"

xcode - 如何在 swift 中取消选择 TextField 的内容

ios - Firebase 或 Swift 未检测变音符号

iphone - 如何获取UIButton的标签号

javascript - 如何使用蓝牙键盘在 iPad 和 iPhone 上玩 javascript 游戏?

ios - ios 4 中的导航栏自定义在 ios5 中不起作用

ios - Realm 对象在写入前获取 `invalidated`,仅在第一次运行时