Demo:http://download.csdn.net/detail/u012881779/8648957
atom
1.更改搜索欄樣式:
spa
#import "ViewController.h" #import "SearchBar.h" @interface ViewController () <UISearchBarDelegate,UITextFieldDelegate> @property (weak, nonatomic) IBOutlet UISearchBar *searchView; @property (strong, nonatomic) UIView *gInView; //內邊框 @end @implementation ViewController @synthesize gInView = _gInView; - (void)viewDidLoad { [super viewDidLoad]; //改變searchBar樣式 [self cSearchBar]; //自定義UISearchBar SearchBar *search = [[SearchBar alloc] initWithFrame:CGRectMake(0, 300, self.view.frame.size.width, 44)]; [self.view addSubview:search]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.view endEditing:YES]; } //搜索欄 -(void)cSearchBar{ float height = _searchView.frame.size.height; float width = _searchView.frame.size.width; //提示文本顏色 UITextField *searchField = [_searchView valueForKey:@"_searchField"]; [searchField setTextColor:[UIColor blackColor]]; [searchField setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"]; [searchField setFont:[UIFont systemFontOfSize:14]]; [searchField setBackgroundColor:[UIColor whiteColor]]; [_searchView setPlaceholder:@"輸入關鍵字搜索"]; [_searchView setBackgroundColor:[UIColor clearColor]]; //外背景 UIView *outV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)]; [outV setBackgroundColor:[UIColor clearColor]]; //內邊框 if(!_gInView){ _gInView = [[UIView alloc] init]; } [_gInView setFrame:CGRectMake(7, 6, width-14, height-12)]; [_gInView setBackgroundColor:[UIColor clearColor]]; [_gInView.layer setBorderWidth:1]; [_gInView.layer setCornerRadius:6]; CGColorRef colorref = [[UIColor colorWithRed:200.0/255 green:200.0/255 blue:200.0/255 alpha:1] CGColor]; [_gInView.layer setBorderColor:colorref]; if([[UIDevice currentDevice].systemVersion floatValue] >= 7.0){ [_gInView setHidden:NO]; }else{ [_gInView setHidden:YES]; } [outV addSubview:_gInView]; [outV setBackgroundColor:[UIColor whiteColor]]; [_searchView insertSubview:outV atIndex:1]; } #pragma mark UISearchBarDelegate - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{ CGColorRef colorref = [[UIColor colorWithRed:34.0/255 green:129.0/255 blue:203.0/255 alpha:1] CGColor]; [_gInView.layer setBorderColor:colorref]; return YES; } //完成編輯 - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{ CGColorRef colorref = [[UIColor colorWithRed:200.0/255 green:200.0/255 blue:200.0/255 alpha:1] CGColor]; [_gInView.layer setBorderColor:colorref]; [self.view endEditing:YES]; return YES; } //搜索按鍵 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{ [self.view endEditing:YES]; } @end
2.重寫UISearchBar: .net
#import <UIKit/UIKit.h> @interface SearchBar : UISearchBar @end #import "SearchBar.h" @implementation SearchBar - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code self.tintColor = [UIColor whiteColor]; } return self; } -(void) layoutSubviews { [super layoutSubviews]; UITextField *searchField; NSArray *subviewArr = self.subviews; for(int i = 0; i < subviewArr.count ; i++) { UIView *viewSub = [subviewArr objectAtIndex:i]; NSArray *arrSub = viewSub.subviews; for (int j = 0; j < arrSub.count ; j ++) { id tempId = [arrSub objectAtIndex:j]; if([tempId isKindOfClass:[UITextField class]]) { searchField = (UITextField *)tempId; } } } //自定義UISearchBar if(searchField) { searchField.placeholder = @"輸入要查找的關鍵字"; [searchField setBorderStyle:UITextBorderStyleRoundedRect]; [searchField setBackgroundColor:[UIColor blueColor]]; [searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"]; [searchField setTextColor:[UIColor orangeColor]]; //本身的搜索圖標 NSString *path = [[NSBundle mainBundle] pathForResource:@"search1" ofType:@"png"]; UIImage *image = [UIImage imageWithContentsOfFile:path]; UIImageView *iView = [[UIImageView alloc] initWithImage:image]; [iView setFrame:CGRectMake(0.0, 0.0, 16.0, 16.0)]; searchField.leftView = iView; } //外部背景 UIView *outView = [[UIView alloc] initWithFrame:self.bounds]; [outView setBackgroundColor:[UIColor orangeColor]]; [self insertSubview:outView atIndex:1]; } @end示圖: