我正在使用UItableView控制器,我无法添加搜索栏到导航栏。我希望搜索栏来时,搜索按钮的导航栏被点击。如何添加搜索按钮和搜索栏上的导航栏。
92dk7w1h1#
在按钮单击处添加此文件=〉.m文件:
UISearchBar *sBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,10,self.navigationController.navigationBar.bounds.size.width,self.navigationController.navigationBar.bounds.size.height/2)]; sBar.delegate = self; [self.navigationController.navigationBar addSubview:sBar];
并添加此=〉.h文件
@interface ViewController : UIViewController<UISearchBarDelegate>
kgsdhlau2#
要使搜索栏像这样你要插入按钮在导航栏控制器和设置为search.png的背景图像(你的形象).所以,当用户点击这个按钮设置为searchbar的目标将被打开.请检查下面的代码为您的参考.首先在.h文件中设置委托方法。
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;
然后在导航栏中插入按钮。
- (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你的按钮方法。
clickme
- (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]; }
当你点击搜索图标
qlzsbp2j3#
对于搜索按钮,如果需要,可以使用UIBarButtonSystemItemSearch。
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(actionSearch:)];
并且你可以设置搜索栏为导航栏的标题视图
self.navigationItem.titleView = mySearchBar;
3条答案
按热度按时间92dk7w1h1#
在按钮单击处添加此文件=〉.m文件:
并添加此=〉.h文件
kgsdhlau2#
要使搜索栏像这样你要插入按钮在导航栏控制器和设置为
search.png
的背景图像(你的形象).所以,当用户点击这个按钮设置为searchbar
的目标将被打开.请检查下面的代码为您的参考.首先在
.h
文件中设置委托方法。然后在导航栏中插入按钮。
现在你必须在
clickme
上设置searchcontroller你的按钮方法。当你点击搜索图标
qlzsbp2j3#
对于搜索按钮,如果需要,可以使用UIBarButtonSystemItemSearch。
并且你可以设置搜索栏为导航栏的标题视图