UISearchBar也是IOS開辟經常使用控件之一,點出來看看外面的屬性barStyle、text、placeholder等等。然則這些屬性明顯缺乏矣知足我們的開辟需求。好比:修正placeholder的色彩、修正UISearchBar下面的UITextfield的配景色彩、修正UITextfield下面的照片等等。
為了完成上述的需求,最好寫一個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)) {
//設置色彩
searchField.textColor = [UIColor whiteColor];
//設置配景色彩
[searchField setBackground: [UIImage imageNamed:@"searchbar"] ];
[searchField setBorderStyle:UITextBorderStyleNone];
//設置placeholder的色彩
[searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
//設置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是由兩個subView構成的,一個是UISearchBarBackGround,另外一個是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;
}
}
}
或可以設置的tintcolor實用於症結在search bar。 應用tintColor至著色遠景 應用barTintColor要著色的欄配景。 在IOS體系V7.0,的UIView的子類派生的基類行動tintColor。見tintColor在為UIView的程度 蘋果文件
2. 可以經由過程設置文字的色彩
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];
3. 固然這是真的,UIAppearance協定是一個“地下的API,”這不是真的,UITextField的支撐這一點。 假如你看一看UITextField.h並查找字符串“UI_APPEARANCE_SELECTOR”,你會看到它有這個字符串的任何實例。假如你看的UIButton CodeGo.net,你會發明很多-這些都是由該UIAppearance API正式支撐的屬性。這是盡人皆知的,UITextField的是不支撐的UIAppearance API,所以在桑迪普的謎底代碼其實不老是可行的,它現實上不是最好的辦法。 這是帖子的鏈接: 准確的做法是-遍歷子視圖(或子視圖重要用於IOS7的子視圖)和手動設置。不然,您將有弗成靠的成果。但你可以創立一個種別的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];
}
【iOS中的UISearchBar搜刮框組件基本應用指南】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!