开发应用实现搜索页面就需要创建搜索框,为了满足设计师的需求可能需要自定义搜索框,既然 Objective-C 为我们提供了 UISearchBar,原生控件提供了完整的接口供开发者调用,我们是否可以通过对原生控件的继承或者添加类目的方式修改 UISearchBar 的样式来满足我们的需求呢?
为了方便理解小主用最通俗的代码进行分析:
#import <UIKit/UIKit.h>
@interface WJKSearchBar : UISearchBar
@end
#import "WJKSearchBar.h"
@implementation WJKSearchBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.tintColor = [UIColor whiteColor];
self.backgroundColor = [UIColor clearColor];
}
return self;
}
-(void) layoutSubviews
{
[super layoutSubviews];
UITextField *searchField = nil;
for (UIView *subview in self.subviews) {
for(UIView *moduleView in subview.subviews){
if ([moduleView isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
moduleView.alpha = 0.3f;
}else if([moduleView isKindOfClass:NSClassFromString(@"UISearchBarTextField")] ){
searchField = (UITextField *)moduleView;
}else{
moduleView.alpha = 0.0f;
}
}
}
if(searchField) {
searchField.textAlignment = NSTextAlignmentLeft;
searchField.tintColor = [UIColor whiteColor];
searchField.layer.cornerRadius = 15;
searchField.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
[searchField setBorderStyle:UITextBorderStyleRoundedRect];
[searchField setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.3]];
[searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
[searchField setTextColor:[UIColor whiteColor]];
}
}
@end
- (void)_setUpSearchBar {
UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 27)];
if (@available(iOS 11.0, *)) {
self.searchBar = [[WJKSearchBar alloc] initWithFrame:CGRectMake(8, 0, SCREENWIDTH - 70, 28)];
} else {
self.searchBar = [[WJKSearchBar alloc] initWithFrame:CGRectMake(-30, 0, SCREENWIDTH - 70, 28)];
}
self.searchBar.placeholder = @"搜索感兴趣的内容";
[[self searchBar] setImage:[UIImage imageNamed:@"home_search"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
self.searchBar.font = [UIFont systemFontOfSize:14];
self.searchBar.delegate = self;
self.searchBar.layer.cornerRadius = 15;
self.searchBar.layer.masksToBounds = YES;
[titleView addSubview:[self searchBar]];
self.navigationItem.titleView = titleView;
}
通过类目进行扩充:
#import <UIKit/UIKit.h>
@interface UISearchBar (Categories)
@property (strong, nonatomic, readonly) UITextField* textField;
@property (strong, nonatomic) UIColor* textColor;
@property (strong, nonatomic) UIFont* font;
@property (strong, nonatomic) UIColor* backgroundColor;
@property (strong, nonatomic) UIColor* placeholderColor;
@end
#import "UISearchBar+Categories.h"
#import "CategoryMacros.h"
CategoryKit_LoadCategory(UISearchBar_Categories)
@implementation UISearchBar (Categories)
- (UITextField*)textFieldInView:(UIView *)view;{
for (UIView *subview in [view subviews]){
if ([subview isKindOfClass:[UITextField class]]){
return (UITextField*)subview;
} else if ([subview subviews] && [[subview subviews] count]){
UITextField *textField = [self textFieldInView:subview];
if (textField) {
return textField;
}
}
}
return nil;
}
- (UITextField*)textField;{
return [self textFieldInView:self];
}
- (void)setFont:(UIFont *)font{
[[self textField] setFont:font];
}
- (UIFont*)font{
return [[self textField] font];
}
- (void)setTextColor:(UIColor *)textColor{
[[self textField] setTextColor:textColor];
}
- (UIColor*)textColor{
return [[self textField] textColor];
}
- (void)setBackgroundColor:(UIColor *)backgroundColor{
[[self textField] setBackgroundColor:backgroundColor];
}
- (UIColor *)backgroundColor{
return [[self textField] backgroundColor];
}
- (void)setPlaceholderColor:(UIColor *)placeholderColor{
[[self textField] setValue:placeholderColor forKeyPath:@"_placeholderLabel.textColor"];
}
- (UIColor *)placeholderColor{
return [[self textField] valueForKeyPath:@"_placeholderLabel.textColor"];
}
@end
仅提供部分思路,满足自己的开发需求即可。