关于自定义的UISearchBar的背景图设置,在经过自己查找资料的情况下,发现的问题是,只有代码段,其采用的方式是,重写UISearchBar,然后调用layoutSubviews这个方法。
那么关于自定UISearchBar我采用的方法是类似自定义UITableviewCell的方法,那么接下来这个layoutSubviews的方法,我想大家也就知道该在那里实现了,实现代码如下:
personSearch.h1 2 3 4 5 6 7 8 9 10 11 12 13
|
#import <UIKit/UIKit.h>
@interface personSearch : UISearchBar
@end
|
personSearch.m1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#import "personSearch.h" #import <QuartzCore/QuartzCore.h>
@implementation personSearch
- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.tintColor = [UIColor whiteColor]; } return self; }
-(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.placeholder = @"输入要查找的艺人的名字";
[searchField setBorderStyle:UITextBorderStyleRoundedRect]; [searchField setBackgroundColor:[UIColor whiteColor]];
NSString *path = [[NSBundle mainBundle] pathForResource:@"GUI_search" ofType:@"png"]; UIImage *image = [UIImage imageWithContentsOfFile:path]; UIImageView *iView = [[UIImageView alloc] initWithImage:image]; [iView setFrame:CGRectMake(0.0, 0.0, 30.0, 30.0)]; searchField.leftView = iView;
} [super layoutSubviews]; }
@end
|
OK!实现过程就是这样的,漂亮的结果就出来了