カスタムUsearchBar
5362 ワード
デモ:http://download.csdn.net/detail/u012881779/8648957
1.検索バーのスタイルを変更する:
1.検索バーのスタイルを変更する:
#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.UICSearch Barを書き換える:#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
図: