最近须要开发搜索框,比较了之前的各个版本的搜索框,UISearchDisplayController和UISearchController的使用,之后再作记录,随着ios11的更新,一些控件发生了,改变,不过也不影响咱们,这里我根据大神封装的PYSearchViewController,从新小小的封装了下,加了个自定义导航,效果图以下:html
大神封装的已经很好了,具体代码以下:ios
在大神封装的类库里面的PYSearchViewController类的第251行,添加以下代码;git
//改变导航背景颜色 [UINavigationBar appearance].barTintColor = MainColor;
和上面的那个方法,能够本身自定义导航栏的背景颜色,和字体的颜色!github
分享一个自定义导航,没什么两点,只是作记录用:app
.hide
// // CustomNavigationBar.h // CBS // // Created by Hero11223 on 16/6/23. // Copyright © 2016年 zyy. All rights reserved. // #import <UIKit/UIKit.h> //这里设个枚举 typedef enum:NSUInteger { BtnTypeSave,//保存 BtnTypeMore,//更多 BtnTypeClist,//菜单 BtnTypeSearch,//搜索 BtnTypeAdd,//添加 BtnTypeCamera,//添加动态 BtnTypeDetail }RightBtnType; @protocol CustomNavigationBarDelegate<NSObject> @optional -(void)leftBtnAction:(id)sender; -(void)rightBtnAction:(id)sender; @end @interface CustomNavigationBar : UIView @property(nonatomic,strong)UILabel *titleLabel;//标题 @property(nonatomic,strong)UIButton *leftButton;//左边的按钮 @property(nonatomic,strong)UIButton *rightButton;//右边的按钮 @property(nonatomic,assign)RightBtnType rightBtnType; @property(nonatomic,assign)BOOL isRightHidden; @property(nonatomic,strong)UIImageView *leftImg;//左边的图片 @property(nonatomic,strong)id<CustomNavigationBarDelegate>delegate;//声明代理 //声明两个方法 -(id)initWithFrame:(CGRect)frame withTitle:(NSString *)title withLeftBtnHidden:(BOOL)l_hidden withRightBtn:(BOOL)r_hidden; //-(id)initWithFrame:(CGRect)frame withTitle:(NSString *)title withLeftBtnHidden:(BOOL)l_hidden withRightBtn:(BOOL)r_hidden leftWidth:(CGFloat)l_width rightWidth:(CGFloat)r_width; -(id)initWithFrametwo:(CGRect)frame withTitle:(NSString *)title withLeftBtnHidden:(BOOL)l_hidden withRightBtn:(BOOL)r_hidden; -(void)liftBtn:(NSString *)name; @end
.m字体
// // CustomNavigationBar.m // CBS // // Created by Hero11223 on 16/6/23. // Copyright © 2016年 zyy. All rights reserved. // //主色调 #define MainColor [UIColor colorWithRed:0.0/255.0 green:185.0/255.0 blue:239.0/255.0 alpha:1.0] #define KscreenW [UIScreen mainScreen].bounds.size.width #define KscreenH [UIScreen mainScreen].bounds.size.height #import "CustomNavigationBar.h" @implementation CustomNavigationBar /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ -(id)initWithFrame:(CGRect)frame withTitle:(NSString *)title withLeftBtnHidden:(BOOL)l_hidden withRightBtn:(BOOL)r_hidden { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = MainColor; //中间的标题 self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(75.0, frame.size.height - 44, KscreenW-75.0*2, 44)]; self.titleLabel.backgroundColor = [UIColor clearColor]; self.titleLabel.textColor = [UIColor whiteColor]; self.titleLabel.font = [UIFont boldSystemFontOfSize:18.0]; self.titleLabel.text = title; self.titleLabel.textAlignment = NSTextAlignmentCenter; [self addSubview:self.titleLabel]; //左边的按钮 _leftButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_leftButton addTarget:self action:@selector(clickLeftBtn:) forControlEvents:UIControlEventTouchUpInside]; _leftButton.frame = CGRectMake(0, frame.size.height-44, 44, 44); [_leftButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal]; [self addSubview:_leftButton]; _leftButton.hidden = l_hidden; //右边的按钮 _rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_rightButton addTarget:self action:@selector(clickRightBtn:) forControlEvents:UIControlEventTouchUpInside]; _rightButton.frame = CGRectMake(frame.size.width - 57, frame.size.height - 44, 44, 44); [_rightButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal]; _rightButton.hidden = r_hidden; [self addSubview:_rightButton]; }; return self; } -(id)initWithFrametwo:(CGRect)frame withTitle:(NSString *)title withLeftBtnHidden:(BOOL)l_hidden withRightBtn:(BOOL)r_hidden { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = MainColor; //中间的标题 self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(75.0, frame.size.height - 44, KscreenW-75.0*2, 44)]; self.titleLabel.backgroundColor = [UIColor clearColor]; self.titleLabel.textColor = [UIColor whiteColor]; self.titleLabel.font = [UIFont boldSystemFontOfSize:18.0]; self.titleLabel.text = title; self.titleLabel.textAlignment = NSTextAlignmentCenter; [self addSubview:self.titleLabel]; //左边的按钮 _leftButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_leftButton addTarget:self action:@selector(clickLeftBtn:) forControlEvents:UIControlEventTouchUpInside]; _leftButton.frame = CGRectMake(30, frame.size.height-44, 44, 44); // [_leftButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal]; [self addSubview:_leftButton]; _leftButton.hidden = l_hidden; //右边的按钮 _rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_rightButton addTarget:self action:@selector(clickRightBtn:) forControlEvents:UIControlEventTouchUpInside]; _rightButton.frame = CGRectMake(frame.size.width - 57, frame.size.height - 44, 44, 44); [_rightButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal]; _rightButton.hidden = r_hidden; [self addSubview:_rightButton]; //左边按钮的图片 _leftImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"nav"]]; _leftImg.frame = CGRectMake(10, frame.size.height-34, 15, 20); [self addSubview:_leftImg]; }; return self;} -(void)setIsRightHidden:(BOOL)isRightHidden { if (isRightHidden == YES) { CGRect rect = self.titleLabel.frame; rect.size.width = KscreenW - 75.0*2; self.titleLabel.frame = rect; }else { CGRect rect = self.titleLabel.frame; rect.size.width = KscreenW - 75.0-120.0; self.titleLabel.frame = rect; } } -(void)liftBtn:(NSString *)name { [_leftButton setTitle:name forState:UIControlStateNormal]; [_leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; _leftButton.titleLabel.font = [UIFont systemFontOfSize:14]; [_leftButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal]; _leftImg.hidden = NO; } -(void)setRightBtnType:(RightBtnType)rightBtnType { switch (rightBtnType) { case BtnTypeSave://保存 { [_rightButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal]; [_rightButton setTitle:@"保存" forState:UIControlStateNormal]; [_rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [_rightButton setImageEdgeInsets:UIEdgeInsetsMake(12, 16, 12, 4)]; _rightButton.titleLabel.font = [UIFont systemFontOfSize:17.0]; } break; case BtnTypeMore://更多 { [_rightButton setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal]; [_rightButton setTitle:@"" forState:UIControlStateNormal]; [_rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [_rightButton setImageEdgeInsets:UIEdgeInsetsMake(6, 7, 10, 8)]; _rightButton.titleLabel.font = [UIFont systemFontOfSize:17.0]; } break; case BtnTypeClist://菜单 { CGRect rect = _rightButton.frame; rect.origin.x = KscreenW - 57.0; rect.size.width = 44.0; _rightButton.frame = rect; [_rightButton setImage:[UIImage imageNamed:@"title_bar_add"] forState:UIControlStateNormal]; [_rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [_rightButton setTitle:@"" forState:UIControlStateNormal]; _rightButton.titleLabel.font = [UIFont systemFontOfSize:17.0]; } break; case BtnTypeSearch://搜索 { CGRect rect = _rightButton.frame; rect.origin.x = KscreenW - 40.0; rect.origin.y = 30; rect.size.width = 22; rect.size.height = 22; _rightButton.frame = rect; [_rightButton setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal]; [_rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [_rightButton setTitle:@"" forState:UIControlStateNormal]; _rightButton.titleLabel.font = [UIFont systemFontOfSize:17.0]; } break; case BtnTypeAdd://添加 { CGRect rect = _rightButton.frame; rect.origin.x = KscreenW - 57.0; rect.size.width = 30; rect.size.height = 50; _rightButton.frame = rect; // _rightButton = [UIButton buttonWithType:UIButtonTypeContactAdd]; [_rightButton setImage:[UIImage imageNamed:@"加号3"] forState:UIControlStateNormal]; [_rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [_rightButton setTitle:@"" forState:UIControlStateNormal]; _rightButton.titleLabel.font = [UIFont systemFontOfSize:17.0]; } break; case BtnTypeCamera://添加动态 { CGRect rect = _rightButton.frame; rect.origin.x = KscreenW - 40.0; rect.origin.y = 30; rect.size.width = 22; rect.size.height = 22; _rightButton.frame = rect; // _rightButton = [UIButton buttonWithType:UIButtonTypeContactAdd]; [_rightButton setImage:[UIImage imageNamed:@"相机"] forState:UIControlStateNormal]; [_rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [_rightButton setTitle:@"" forState:UIControlStateNormal]; _rightButton.titleLabel.font = [UIFont systemFontOfSize:17.0]; } break; case BtnTypeDetail://添加动态 { [_rightButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal]; [_rightButton setTitle:@"明细" forState:UIControlStateNormal]; [_rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [_rightButton setImageEdgeInsets:UIEdgeInsetsMake(12, 16, 12, 4)]; _rightButton.titleLabel.font = [UIFont systemFontOfSize:17.0]; } break; default: break; } } -(void)clickLeftBtn:(UIButton *)sender { if (self.delegate && [self.delegate respondsToSelector:@selector(leftBtnAction:)]) { [self.delegate leftBtnAction:sender]; } } -(void)clickRightBtn:(UIButton *)sender { if (self.delegate && [self.delegate respondsToSelector:@selector(rightBtnAction:)]) { [self.delegate rightBtnAction:sender]; } } @end
具体调用:atom
1,声明代理spa
@interface ViewController ()<CustomNavigationBarDelegate>
2,引用代理
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.navigationController.navigationBarHidden = YES; CustomNavigationBar *nav = [[CustomNavigationBar alloc]initWithFrame:CGRectMake(0, 0, KscreenW, 64) withTitle:@"首页" withLeftBtnHidden:NO withRightBtn:NO]; nav.delegate = self; nav.rightBtnType = BtnTypeSearch; [self.view addSubview:nav]; }
3,而后实现两个代理方法:
-(void)leftBtnAction:(id)sender { } -(void)rightBtnAction:(id)sender { }
这样就能够了!不过就是我尚未晚上手势返回那个功能,有兴趣的能够研究下,也就一行代码的事!
下面就是搜索框的使用:
1,引入代理
@interface ViewController ()<PYSearchViewControllerDelegate>
2,调用PYSearchViewController
-(void)rightBtnAction:(id)sender { // 1. 建立热门搜索 NSArray *hotSeaches = @[@"Java", @"Python", @"Objective-C", @"Swift", @"C", @"C++", @"PHP", @"C#", @"Perl", @"Go", @"JavaScript", @"R", @"Ruby", @"MATLAB"]; // 2. 建立控制器 PYSearchViewController *searchViewController = [PYSearchViewController searchViewControllerWithHotSearches:hotSeaches searchBarPlaceholder:@"搜索" didSearchBlock:^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) { // 开始搜索执行如下代码 // 如:跳转到指定控制器 [searchViewController.navigationController pushViewController:[[PYTempViewController alloc] init] animated:YES]; }]; // 3. 设置风格 // searchViewController.hotSearchStyle = PYHotSearchStyleNormalTag; // 热门搜索风格根据选择 searchViewController.searchHistoryStyle = PYHotSearchStyleDefault; // 搜索历史风格为default // 5. 跳转到搜索控制器 UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:searchViewController]; [self presentViewController:nav animated:NO completion:nil]; }
3,在搜索结果控制器PYTempViewController里面,会出现一个问题,从这个页面返回的时候,PYSearchViewController页面的导航栏不见了,解决方法以下:
@interface PYTempViewController ()<CustomNavigationBarDelegate> { CustomNavigationBar *customBar;; } @end @implementation PYTempViewController -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [customBar removeFromSuperview]; self.navigationController.navigationBarHidden = NO; } - (void)viewWillAppear:(BOOL)animated { self.navigationController.navigationBarHidden = YES; customBar = [[CustomNavigationBar alloc]initWithFrame:CGRectMake(0, 0, KscreenW, 64) withTitle:@"搜索结果" withLeftBtnHidden:NO withRightBtn:YES]; customBar.delegate = self; [self.view addSubview:customBar]; }
把上述两个方法添加到控制器里面便可!
这样一个搜索框就完成了!很美观,定制型也很高,感谢大神们的无私奉献!
PYSearch传送门:https://github.com/hgl753951/PYSearch.git
UISearchBar全属性传送门:http://www.cnblogs.com/sunfuyou/p/6244378.html
Demo传送门:https://github.com/hgl753951/SearchTest.git
结束!若是有不足之处,请你们必定指出,共同进步!