最近須要開發搜索框,比較了之前的各個版本的搜索框,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
結束!若是有不足之處,請你們必定指出,共同進步!