我可以使用标准iOS UIKit
制作吗?
当我谷歌搜索UISearchBar
时,所有教程都附带UITableView
。这不是我想要的。
我希望此搜索栏为
答案 0 :(得分:0)
要使搜索栏像这样你想在导航栏控制器中插入按钮并将背景图像设置为search.png
(你的图像)。因此,当用户点击此按钮时,目标为searchbar
的目标将被打开。请查看以下代码供您参考。
首先在你.h
文件中设置委托方法。
@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中插入按钮。
- (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];
}
点击搜索图标