查看完整文章请到源地址:http://386502324.blog.163.com/blog/static/113469377201510282124497/
一: 问题描述,为什么要去完全自定义searchBar
相
信许多同学在开发中,会遇到UISearchBar的自订制问题。比如一个简单的placeholder永远居左,且关键字会不断变化,就会逼疯很多人,
使用空格符达到效果。自从iOS7之后,如果不想使用自带的searchBar的默认效果就更容易遇到各种问题。因此,使用textField去完全解决
自定义的方法无疑是最有效率的。
二:思路,如何去自定义searchBar
作为苹果的封装类,searchBar无非是给大家便利的提供了一种使用textField的类。其核心还是textField。因此,我们的封装,也必须是围绕textField来完成的。
1:主体是一个view
2:放大镜图片,可以在主view上添加imageView,也可以使用textField的leftView;
3:取消按钮,完全可以不放到主view中,在外界使用的时候再去自行创建使用。
4:textField
5:重点:代理的实现,无非是将textField的代理无缝结合移植到封装类,可以参考系统的searchbar的代理,也写代理。
6:既然是自封装,属性完全可以多设置一些,方便大家调用。
三:代码示例
下面就是我花了半天时间简单封装的searchBar,虽然还会有些不太完善的地方,但是绝大部分功能都可以正常使用了。可以根据自己的需求,继续完善属性,添加方法,甚至代理方法。先是.h中。
//
// OCSearchBar.h
// OpenCourse
//
// Created by 徐坤 on 15/11/27.
//
// 不让外界直接调用textField,用起来会更像系统的searchBar的API,完善属性后,会将textField拿到.m中
#import <UIKit/UIKit.h>
@class OCSearchBar;
@protocol OCSearchBarDelegate <NSObject>
@optional
- (BOOL)OCSearchBarShouldBeginEditing:(OCSearchBar *)searchBar;
- (void)OCSearchBarTextDidBeginEditing:(OCSearchBar *)searchBar;
- (BOOL)OCSearchBarShouldEndEditing:(OCSearchBar *)searchBar;
- (void)OCSearchBarTextDidEndEditing:(OCSearchBar *)searchBar;
- (void)OCSearchBar:(OCSearchBar *)searchBar textDidChange:(NSString *)searchText;
- (BOOL)OCSearchBar:(OCSearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
- (void)OCSearchBarSearchButtonClicked:(OCSearchBar *)searchBar;
@end
@interface OCSearchBar : UIView <UITextFieldDelegate>
@property (nonatomic, strong) UIImage *backViewImage;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIImageView *leftImageView;//左侧放大镜
@property (nonatomic, copy ) NSString *placeholder;
@property (nonatomic, strong) UIFont *font;
@property (nonatomic, copy) NSString *text;
@property (nonatomic, strong) UIColor *placeholderColor;
@property (nonatomic, strong) UIColor *textColor;
@property (nonatomic, assign) BOOL isFirstResponder;
@property (nonatomic, weak) id <OCSearchBarDelegate>delegate;
- (instancetype)initWithFrame:(CGRect)frame;
- (void)resignFirstResponder;
- (void)becomeFirstResponder;
@end
============然后是实现文件.m=========自行粘贴到自己项目中即可。
//
// OCSearchBar.m
// OpenCourse
//
// Created by 徐坤 on 15/11/27.
//
//
#import "OCSearchBar.h"
@interface OCSearchBar ()
@property (nonatomic, strong) UIImageView *backGroundView;
@end
@implementation OCSearchBar
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
[self setupSubviews];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}
- (void)setupSubviews {
self.backGroundView = [[UIImageView alloc] initWithFrame:self.bounds];
[self addSubview:self.backGroundView];
self.textField = [[UITextField alloc] initWithFrame:self.bounds];
self.textField.backgroundColor = [UIColor clearColor];
self.textField.delegate = self;
self.textField.clearButtonMode = YES;
self.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.textField.returnKeyType = UIReturnKeySearch;
//self.textField.leftViewMode = UITextFieldViewModeAlways;
[self addSubview:self.textField];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(OCSearchBarDidChange:)
name:UITextFieldTextDidChangeNotification object:nil];
[self setupDefaultLeftImageView];
[self setupDefaultBackGroundView];
}
- (void)setupDefaultBackGroundView {
// 使用颜色创建UIImage
CGSize imageSize = self.bounds.size;
UIGraphicsBeginImageContextWithOptions(imageSize, 0, [UIScreen mainScreen].scale);
[RGB(26, 66, 38) set];
UIRectFill(CGRectMake(0, 0, imageSize.width, imageSize.height));
UIImage *colorImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.backGroundView.layer.cornerRadius = 6;
self.backGroundView.layer.masksToBounds = YES;
[self.backGroundView setImage:colorImage];
}
- (void)setupDefaultLeftImageView {
UIImageView *leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search_fdj"]];
[leftImageView setFrame:CGRectMake(13, 6, 18, 18)];
self.leftImageView = leftImageView;
}
- (void)setFont:(UIFont *)font {
self.textField.font = font;
}
- (void)setPlaceholderColor:(UIColor *)placeholderColor {
[self.textField setValue:placeholderColor forKeyPath:@"_placeholderLabel.textColor"];
}
- (void)setTextColor:(UIColor *)textColor {
[self.textField setTextColor:textColor];
}
- (void)setText:(NSString *)text {
self.textField.text = text;
}
- (NSString *)text {
return self.textField.text;
}
- (void)setBackViewImage:(UIImage *)backViewImage {
[self.backGroundView setImage:backViewImage];
}
- (void)setLeftImageView:(UIImageView *)leftImageView {
if (self.leftImageView.superview) {
[self.leftImageView removeFromSuperview];
}
[self addSubview:leftImageView];
CGRect rect = self.textField.bounds;
rect.origin.x = rect.origin.x+leftImageView.frame.origin.x+leftImageView.frame.size.width+5;
rect.size.width = rect.size.width - leftImageView.frame.origin.x - leftImageView.frame.size.width-5;
[self.textField setFrame:rect];
}
- (void)setPlaceholder:(NSString *)placeholder {
self.textField.placeholder = placeholder;
}
- (void)resignFirstResponder {
[self.textField resignFirstResponder];
}
- (void)becomeFirstResponder {
[self.textField becomeFirstResponder];
}
#pragma mark -
#pragma mark - OCTextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (self.delegate && [self.delegate respondsToSelector:@selector(OCSearchBarShouldBeginEditing:)] ) {
return [self.delegate OCSearchBarShouldBeginEditing:self];
}
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.isFirstResponder = YES;
if (self.delegate && [self.delegate respondsToSelector:@selector(OCSearchBarTextDidBeginEditing:)]) {
[self.delegate OCSearchBarTextDidBeginEditing:self];
}
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
if (self.delegate && [self.delegate respondsToSelector:@selector(OCSearchBarShouldEndEditing:)]) {
return [self.delegate OCSearchBarShouldEndEditing:self];
}
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (self.delegate && [self.delegate respondsToSelector:@selector(OCSearchBarTextDidEndEditing:)]) {
[self.delegate OCSearchBarTextDidEndEditing:self];
}
}
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
if (self.delegate && [self.delegate
respondsToSelector:@selector(OCSearchBar:shouldChangeTextInRange:replacementText:)])
{
return [self.delegate OCSearchBar:self shouldChangeTextInRange:range replacementText:string];
}
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField {
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (self.delegate && [self.delegate respondsToSelector:@selector(OCSearchBarSearchButtonClicked:)]) {
[self.delegate OCSearchBarSearchButtonClicked:self];
}
return YES;
}
- (void)OCSearchBarDidChange:(NSNotification *)notification {
UITextField *textField = [notification object];
if (self.delegate && [self.delegate respondsToSelector:@selector(OCSearchBar:textDidChange:)]) {
[self.delegate OCSearchBar:self textDidChange:textField.text];
}
}
@end
使用UITextField去自定义searchBar 【iOS】
原文:http://www.cnblogs.com/xukunhenchouchang/p/5002661.html