iOS中的UISearchBar搜索框組件基礎(chǔ)使用指南
UISearchBar也是iOS開發(fā)常用控件之一,點(diǎn)進(jìn)去看看里面的屬性barStyle、text、placeholder等等。但是這些屬性顯然不足矣滿足我們的開發(fā)需求。比如:修改placeholder的顏色、修改UISearchBar上面的UITextfield的背景顏色、修改UITextfield上面的照片等等。
為了實(shí)現(xiàn)上述的需求,最好寫一個(gè)UISearchBar的子類就叫LSSearchBar吧
LSSearchBar.h如下:
#import <UIKit/UIKit.h>
@interface LSSearchBar : UISearchBar
@end
LSSearchBar.m如下:
#import "LSSearchBar.h"
@implementation LSSearchBar
- (void)layoutSubviews {
[super layoutSubviews];
//通過遍歷self.subviews找到searchField
UITextField *searchField;
NSUInteger numViews = [self.subviews count];
for(int i = 0; i < numViews; i++) {
if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) {
searchField = [self.subviews objectAtIndex:i];
}
}
//如果上述方法找不到searchField,那就試試下面的方法吧
if (searchField == nil) {
NSArray *arraySub = [self subviews];
UIView *viewSelf = [arraySub objectAtIndex:0];
NSArray *arrayView = [viewSelf subviews];
for(int i = 0; i < arrayView.count; i++) {
if([[arrayView objectAtIndex:i] isKindOfClass:[UITextField class]]) {
searchField = [arrayView objectAtIndex:i];
}
}
}
if(!(searchField == nil)) {
//設(shè)置顏色
searchField.textColor = [UIColor whiteColor];
//設(shè)置背景顏色
[searchField setBackground: [UIImage imageNamed:@"searchbar"] ];
[searchField setBorderStyle:UITextBorderStyleNone];
//設(shè)置placeholder的顏色
[searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
//設(shè)置searchField上的照片
UIImage *image = [UIImage imageNamed:@"search"];
UIImageView *iView = [[UIImageView alloc] initWithImage:image];
iView.frame = CGRectMake(0, 0, 15, 15);
searchField.leftView = iView;
}
}
@end
修改UISearchBar背景顏色
ISearchBar是由兩個(gè)subView組成的,一個(gè)是UISearchBarBackGround,另一個(gè)是UITextField. 要IB中沒有直接操作背景的屬性。方法是直接將 UISearchBarBackGround移去
seachBar=[[UISearchBar alloc] init];
seachBar.backgroundColor=[UIColor clearColor];
for (UIView *subview in seachBar.subviews){
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}
UISearchBar文字顏色改變
1. 在iOS的7訪問文本字段,你必須在水平重申更多。更改您的代碼像這樣
for (UIView *subView in self.searchBar.subviews)
{
for (UIView *secondLevelSubview in subView.subviews){
if ([secondLevelSubview isKindOfClass:[UITextField class]])
{
UITextField *searchBarTextField = (UITextField *)secondLevelSubview;
//set font color here
searchBarTextField.textColor = [UIColor blackColor];
break;
}
}
}
或可以設(shè)置的tintcolor適用于關(guān)鍵在search bar。 使用tintColor至著色前景 使用barTintColor要著色的欄背景。 在iOS系統(tǒng)V7.0,的UIView的子類派生的基類行為tintColor。見tintColor在為UIView的水平 蘋果文件
2. 可以通過設(shè)置文字的顏色
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];
3. 雖然這是真的,UIAppearance協(xié)議是一個(gè)“公開的API,”這不是真的,UITextField的支持這一點(diǎn)。 如果你看一看UITextField.h并查找字符串“UI_APPEARANCE_SELECTOR”,你會(huì)看到它有這個(gè)字符串的任何實(shí)例。如果你看的UIButton CodeGo.net,你會(huì)發(fā)現(xiàn)不少-這些都是由該UIAppearance API正式支持的屬性。這是眾所周知的,UITextField的是不支持的UIAppearance API,所以在桑迪普的答案代碼并不總是可行的,它實(shí)際上不是最好的方法。 這是帖子的鏈接: 正確的做法是-遍歷子視圖(或子視圖主要用于IOS7的子視圖)和手動(dòng)設(shè)置。否則,您將有不可靠的結(jié)果。但你可以創(chuàng)建一個(gè)類別的UISearchBar并添加setTextColor:(*的UIColor)示例:
- (void)setTextColor:(UIColor*)color
{
for (UIView *v in self.subviews)
{
if([Environment isVersion7OrHigher]) //checks UIDevice#systemVersion
{
for(id subview in v.subviews)
{
if ([subview isKindOfClass:[UITextField class]])
{
((UITextField *)subview).textColor = color;
}
}
}
else
{
if ([v isKindOfClass:[UITextField class]])
{
((UITextField *)v).textColor = color;
}
}
}
}
自定義UISearchBar的背景圖
- (void)layoutSubviews {
UITextField *searchField;
NSUInteger numViews = [self.subviews count];
for(int i = 0; i < numViews; i++) {
if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) {
searchField = [self.subviews objectAtIndex:i];
}
}
if(!(searchField == nil)) {
searchField.textColor = [UIColor whiteColor];
[searchField.leftView setHidden:YES];
[searchField setBackground: [UIImage imageNamed:@"SearchBarBackground.png"] ];
[searchField setBorderStyle:UITextBorderStyleNone];
}
[super layoutSubviews];
}
相關(guān)文章
IOS中實(shí)現(xiàn)圖片點(diǎn)擊全屏預(yù)覽
IOS作為一款智能手機(jī)系統(tǒng),在查看圖片的時(shí)候,如果能夠?qū)崿F(xiàn)全屏,對(duì)用戶來說有很好的視覺體驗(yàn),其實(shí)實(shí)現(xiàn)起來非常的簡(jiǎn)單,下面我就結(jié)合一個(gè)簡(jiǎn)單的代碼給大家來分享一下,,需要的朋友可以參考下2015-11-11詳解iOS webview加載時(shí)序和緩存問題總結(jié)
本篇文章主要介紹了iOS webview加載時(shí)序和緩存問題總結(jié) ,這兩天學(xué)習(xí)了Vue.js 感覺組件這個(gè)地方知識(shí)點(diǎn)挺多的,而且很重要,所以,今天添加一點(diǎn)小筆記。2017-09-09IOS開發(fā)Swift?與?OC相互調(diào)用詳解
這篇文章主要為大家介紹了IOS開發(fā)Swift?與?OC相互調(diào)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08objc方法聲明和實(shí)現(xiàn)由于參數(shù)類型不一致所引發(fā)的崩潰
這篇文章主要為大家介紹了objc方法聲明和實(shí)現(xiàn)由于參數(shù)類型不一致所引發(fā)的崩潰詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03iOS開發(fā)藍(lán)牙技術(shù)應(yīng)用增加無線連接功能
這篇文章主要為大家介紹了iOS開發(fā)藍(lán)牙技術(shù)應(yīng)用增加無線連接功能示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02iOS開發(fā)教程之登錄與訪客的邏輯實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于iOS開發(fā)教程之登錄與訪客的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04