iOS自定义简单的搜索框

.m
#import "NewSearchBar.h"

#import "UIColor+AddColor.h"
@implementation NewSearchBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [ super initWithFrame:frame];
    if (self) {
    
        _whiteView = [[ UIView alloc]init];
        [self addSubview:_whiteView];
        
        _searBarImageView = [[ UIImageView alloc]init];
        [_whiteView addSubview:_searBarImageView];
        
        _searchField = [[ UITextField alloc]init];
        [_whiteView addSubview:_searchField];
        
        _cancelBtn = [ UIButton buttonWithType:UIButtonTypeRoundedRect];
        [self addSubview:_cancelBtn];
    
    
        
    }
    
    return self;
}

- (void)layoutSubviews
{
    
  
    _whiteView.frame = CGRectMake(5, 7, self.bounds.size.width - 5 - 30 - 30, 30);
    
    _searBarImageView.frame =CGRectMake(15, 7.5 ,15, 15);
    _searchField.frame =CGRectMake(40, 7.5 ,_whiteView.bounds.size.width - 45, 15);
 
     _cancelBtn.frame =CGRectMake(self.bounds.size.width  - 30 -15  , 7.5 ,30, 30);
    
    self.backgroundColor = [ UIColor colorFromHexCode:@"008ff3"];

    _whiteView.backgroundColor =[ UIColor whiteColor];
    _whiteView.layer.cornerRadius = 5;
    
    [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    _cancelBtn.titleLabel.font = [ UIFont systemFontOfSize:15];
    _cancelBtn.tintColor = [ UIColor whiteColor];
    
    //    对于输入框外观的设置
     _searchField.font = [ UIFont systemFontOfSize:13];
    //占位符字体颜色
    _placeHolderColor = [UIColor redColor];
     _searchField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:_placeHolder attributes:@{NSForegroundColorAttributeName: _placeHolderColor}];
     //    竖线的颜色
    _searchField.tintColor =[ UIColor yellowColor];
    _searchField.clearButtonMode = UITextFieldViewModeAlways;
    
  
    
}

- (void)setPlaceHolder:(NSString *)placeHolder
{
    if (_placeHolder != placeHolder) {
        _placeHolder = placeHolder;
    }

    _searchField.placeholder = _placeHolder;
    
    
    
}























@end
.h
#import 

@interface NewSearchBar : UIView

- (instancetype)initWithFrame:(CGRect)frame;

/**
 *  搜索文本框
 */
@property(nonatomic,strong)UITextField *searchField;
/**
 *  圆角背景
 */
@property(nonatomic,retain)UIView *whiteView;
/**
 * 搜索图标
 */
@property(nonatomic,retain)UIImageView *searBarImageView;
/**
 * 取消按钮
 */
@property(nonatomic,retain)UIButton *cancelBtn;
/**
 *  输入提示
 */
@property(nonatomic,strong)NSString *placeHolder;

/**
 *  输入提示
 */
@property(nonatomic,strong)UIColor *placeHolderColor;

- (void)setPlaceHolder:(NSString *)placeHolder;


@end





你可能感兴趣的:(自定义控件)