iOS UISearchBar 修改背景樣式,修改cancel按鈕顏色 placehold左對(duì)齊

Paste_Image.png

最近公司要求開發(fā)一個(gè)新項(xiàng)目公般,使用到了UISearchBar進(jìn)行地址搜索万搔。

要求如下:


Paste_Image.png

原生的UISearchBar樣式不能滿足UI設(shè)計(jì)需求,網(wǎng)上找了大量資料才解決了這個(gè)問題官帘。

Paste_Image.png

新建JGSearchTextField 繼承 UISearchBar

.h文件

#import <UIKit/UIKit.h>

@interface JGSearchTextField : UISearchBar
@property (nonatomic, assign, setter = setHasCentredPlaceholder:) BOOL hasCentredPlaceholder;

@end

.m文件


#import "JGSearchTextField.h"

@implementation JGSearchTextField

// ------------------------------------------------------------------------------------------
#pragma mark - Initializers
// ------------------------------------------------------------------------------------------
- (instancetype)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]))
    {
        self.hasCentredPlaceholder = NO;
    }
    
    return self;
}

// ------------------------------------------------------------------------------------------
#pragma mark - Methods
// ------------------------------------------------------------------------------------------
- (void)setHasCentredPlaceholder:(BOOL)hasCentredPlaceholder
{
    _hasCentredPlaceholder = hasCentredPlaceholder;
    
    SEL centerSelector = NSSelectorFromString([NSString stringWithFormat:@"%@%@", @"setCenter", @"Placeholder:"]);
    if ([self respondsToSelector:centerSelector])
    {
        NSMethodSignature *signature = [[UISearchBar class] instanceMethodSignatureForSelector:centerSelector];
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
        [invocation setTarget:self];
        [invocation setSelector:centerSelector];
        [invocation setArgument:&_hasCentredPlaceholder atIndex:2];
        [invocation invoke];
    }
    
}
@end

在控制器中使用JGSearchTextField


 //語音搜索按鈕
    UIButton *voiceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [voiceBtn setBackgroundImage:[UIImage imageNamed:@"search_ico_voice"] forState:UIControlStateNormal];
    
    [self.navigationBar addSubview:voiceBtn];
    
    self.searchBar = [[JGSearchTextField alloc] init];
    
    self.searchBar.barStyle     = UIBarStyleBlackOpaque;
    
    self.searchBar.translucent  = YES;
    
    self.searchBar.delegate     = self;
    
    self.searchBar.placeholder = @"請(qǐng)輸入地址或地區(qū)";
    
    self.searchBar.keyboardType = UIKeyboardTypeDefault;
    
    UIImage* searchBarBg = [self GetImageWithColor:[UIColor clearColor] andHeight:30.0f];
    
    //設(shè)置背景圖片
    [_searchBar setBackgroundImage:searchBarBg];
    
    //設(shè)置背景色
    [_searchBar setBackgroundColor:[UIColor clearColor]];
    
    //設(shè)置文本框背景
    [_searchBar setSearchFieldBackgroundImage:searchBarBg forState:UIControlStateNormal];
    
    [_searchBar setContentMode:UIViewContentModeLeft];

    UITextField *searchField = [_searchBar valueForKey:@"_searchField"];
    
    UIImage *image = [UIImage imageNamed:@"search_ico_search"];
    
    UIImageView *iconView = [[UIImageView alloc] initWithImage:image];
    
    iconView.frame = CGRectMake(0, 0, image.size.width , image.size.height);
    
    searchField.leftView = iconView;
    
    searchField.textAlignment = NSTextAlignmentLeft;
    
    [self.navigationBar addSubview:self.searchBar];
    
    [voiceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.searchBar.mas_right).offset(5);
        make.size.mas_equalTo(CGSizeMake(15, 15));
        make.centerY.equalTo(self.searchBar);
    }];
    
        [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.navigationBar).offset(35);
        make.bottom.equalTo(self.navigationBar).offset(-10);
        make.width.mas_equalTo(SCREEN_WIDTH*0.7);
        make.height.mas_equalTo(30);
    }];
    
    //設(shè)置搜索框取消按鈕顏色
    NSMutableDictionary* dic = [NSMutableDictionary dictionary];
    
    dic[NSForegroundColorAttributeName] = HexRGB(0x0aa676);
    
    [[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil]setTitleTextAttributes:dic forState:UIControlStateNormal];
    
    UIView *bottomLine = [[UIView alloc]init];
    
    bottomLine.backgroundColor = HexRGB(0x0aa676);
    
    [self.navigationBar addSubview:bottomLine];
    
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.searchBar).offset(15);
        make.right.equalTo(voiceBtn);
        make.bottom.equalTo(self.navigationBar).offset(-5);
        make.height.mas_equalTo(1);
    }];
    
    self.navigationBar.rightNormalImage=[UIImage imageNamed:@"search_ico_cbb"];
    
    self.navigationBar.rightClickBlock=^{
        
    };


/**
 *  生成圖片
 *
 *  @param color  圖片顏色
 *  @param height 圖片高度
 *
 *  @return 生成的圖片
 */
- (UIImage*) GetImageWithColor:(UIColor*)color andHeight:(CGFloat)height
{
    CGRect r= CGRectMake(0.0f, 0.0f, 1.0f, height);
    UIGraphicsBeginImageContext(r.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, r);
    
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return img;
}

最后初始化 UISearchDisplayController,遵循代理完成了地址搜索功能


- (void)initSearchDisplay
{
    self.displayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
    self.displayController.delegate                = self;
    self.displayController.searchResultsDataSource = self;
    self.displayController.searchResultsDelegate   = self;
    
    [self setExtraCellLineHidden:self.displayController.searchResultsTableView];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末瞬雹,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子刽虹,更是在濱河造成了極大的恐慌酗捌,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,651評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件涌哲,死亡現(xiàn)場(chǎng)離奇詭異胖缤,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)阀圾,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門哪廓,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人初烘,你說我怎么就攤上這事涡真。” “怎么了账月?”我有些...
    開封第一講書人閱讀 162,931評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵综膀,是天一觀的道長。 經(jīng)常有香客問我局齿,道長剧劝,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,218評(píng)論 1 292
  • 正文 為了忘掉前任抓歼,我火速辦了婚禮讥此,結(jié)果婚禮上拢锹,老公的妹妹穿的比我還像新娘。我一直安慰自己萄喳,他們只是感情好卒稳,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,234評(píng)論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著他巨,像睡著了一般充坑。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上染突,一...
    開封第一講書人閱讀 51,198評(píng)論 1 299
  • 那天捻爷,我揣著相機(jī)與錄音,去河邊找鬼份企。 笑死也榄,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的司志。 我是一名探鬼主播甜紫,決...
    沈念sama閱讀 40,084評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼骂远!你這毒婦竟也來了囚霸?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,926評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤吧史,失蹤者是張志新(化名)和其女友劉穎邮辽,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體贸营,經(jīng)...
    沈念sama閱讀 45,341評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡吨述,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,563評(píng)論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了钞脂。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片揣云。...
    茶點(diǎn)故事閱讀 39,731評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖冰啃,靈堂內(nèi)的尸體忽然破棺而出邓夕,到底是詐尸還是另有隱情,我是刑警寧澤阎毅,帶...
    沈念sama閱讀 35,430評(píng)論 5 343
  • 正文 年R本政府宣布焚刚,位于F島的核電站,受9級(jí)特大地震影響扇调,放射性物質(zhì)發(fā)生泄漏矿咕。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,036評(píng)論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望碳柱。 院中可真熱鬧捡絮,春花似錦、人聲如沸莲镣。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽瑞侮。三九已至的圆,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間半火,已是汗流浹背略板。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留慈缔,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,743評(píng)論 2 368
  • 正文 我出身青樓种玛,卻偏偏與公主長得像藐鹤,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子赂韵,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,629評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理娱节,服務(wù)發(fā)現(xiàn),斷路器祭示,智...
    卡卡羅2017閱讀 134,652評(píng)論 18 139
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,079評(píng)論 25 707
  • Swift版本點(diǎn)擊這里歡迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh閱讀 25,365評(píng)論 7 249
  • 奶奶沒上過學(xué) 不識(shí)字 看電視的時(shí)候爺爺總會(huì)把電視上的內(nèi)容重復(fù)說一遍 我從小到大爺爺一直都這樣做 我有時(shí)候不耐煩了 ...
    SMILEMORE閱讀 166評(píng)論 0 1
  • 每個(gè)人的心里,都會(huì)有那么一個(gè)拿得起卻又放不下的存在汇陆。會(huì)想起怒炸,會(huì)掛念,只是卻再也不會(huì)提起毡代。 而在她的記憶里阅羹,L便是那...
    貳姑娘2閱讀 400評(píng)論 0 1