self.customSearchBar = [[UISearchBar alloc] init];
self.customSearchBar.delegate = self;
self.customSearchBar.placeholder = @"请输入查询信息";
//1. 设置背景颜色
//设置背景图是为了去掉上下黑线
self.customSearchBar.backgroundImage = [[UIImage alloc] init];
// 设置SearchBar的颜色主题为白色
self.customSearchBar.barTintColor = [UIColor whiteColor];
//2. 设置圆角和边框颜色
UITextField *searchField = [self.customSearchBar valueForKey:@"searchField"];
if (searchField) {
[searchField setBackgroundColor:[UIColor whiteColor]];
searchField.layer.cornerRadius = 14.0f;
searchField.layer.borderColor = [UIColor colorWithRed:247/255.0 green:75/255.0 blue:31/255.0 alpha:1].CGColor;
searchField.layer.borderWidth = 1;
searchField.layer.masksToBounds = YES;
}
//3. 设置按钮文字和颜色
[self.customSearchBar fm_setCancelButtonTitle:@"取消"];
self.customSearchBar.tintColor = [UIColor colorWithRed:86/255.0 green:179/255.0 blue:11/255.0 alpha:1];
//设置取消按钮字体
[self.customSearchBar fm_setCancelButtonFont:[UIFont systemFontOfSize:22]];
//修正光标颜色
[searchField setTintColor:[UIColor blackColor]];
//4. 设置输入框文字颜色和字体
[self.customSearchBar fm_setTextColor:[UIColor blackColor]];
[self.customSearchBar fm_setTextFont:[UIFont systemFontOfSize:14]];
//5. 设置搜索Icon
[self.customSearchBar setImage:[UIImage imageNamed:@"Search_Icon"]
forSearchBarIcon:UISearchBarIconSearch
state:UIControlStateNormal];
//6. 实现类似微信的搜索框
UIButton *voiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
[voiceButton setImage:[UIImage imageNamed:@"Voice_button_icon"] forState:UIControlStateNormal];
[voiceButton addTarget:self action:@selector(tapVoiceButton:) forControlEvents:UIControlEventTouchUpInside];
[searchField addSubview:voiceButton];
self.voiceButton = voiceButton;
//Autolayout
voiceButton.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *views = NSDictionaryOfVariableBindings(voiceButton);
//设置水平方向约束
[searchField addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[voiceButton(21)]-|" options:NSLayoutFormatAlignAllRight | NSLayoutFormatAlignAllLeft metrics:nil views:views]];
//设置高度约束
[searchField addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[voiceButton(21)]" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:nil views:views]];
//设置垂直方向居中约束
[searchField addConstraint:[NSLayoutConstraint constraintWithItem:voiceButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:searchField attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
#pragma mark -
#pragma mark .....:::::: UISearchBarDelegate ::::::.....
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES animated:YES];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
searchBar.text = @"";
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar endEditing:YES];
}
//监控文本变化
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
self.voiceButton.hidden = searchBar.text.length + (text.length - range.length) > 0;
return YES;
}
#import <UIKit/UIKit.h>
@interface UISearchBar (FMAdd)
- (void)fm_setTextFont:(UIFont *)font;
- (void)fm_setTextColor:(UIColor *)textColor;
- (void)fm_setCancelButtonTitle:(NSString *)title;
/**
* 设置取消按钮字体
*
* @param font 字体
*/
- (void)fm_setCancelButtonFont:(UIFont *)font;
@end
#import "UISearchBar+FMAdd.h"
#define IS_IOS9 [[UIDevice currentDevice].systemVersion doubleValue] >= 9
@implementation UISearchBar (FMAdd)
- (void)fm_setTextFont:(UIFont *)font {
if (IS_IOS9) {
[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]].font = font;
}else {
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:font];
}
}
- (void)fm_setTextColor:(UIColor *)textColor {
if (IS_IOS9) {
[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]].textColor = textColor;
}else {
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:textColor];
}
}
- (void)fm_setCancelButtonTitle:(NSString *)title {
if (IS_IOS9) {
[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitle:title];
}else {
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:title];
}
}
- (void)fm_setCancelButtonFont:(UIFont *)font {
NSDictionary *textAttr = @{NSFontAttributeName : font};
if (IS_IOS9) {
[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:textAttr forState:UIControlStateNormal];
}else {
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:textAttr forState:UIControlStateNormal];
}
}
@end
iOS UISearchBar 设置
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 项目中几乎肯定是要用搜索这项功能的,但是关于搜索框的圆角问题一直在困扰我!所以好好找了一下解决办法,上干货! 1....
- 最近需要优化项目,把搜索框改了,样式如下: 本以为很简单,结果弄一下发现不对,特意把解决方法记录下来: 注意的是叫...
- 更新iOS11后, 不少地方需要进行适配,本文主要讲下UISearchBar的适配,首先我们来看两张图对比 我们发...
- /** 取消searchBar背景色 */ - (UIImage *)imageWithColor:(UIColo...