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
<pre name="code" class="objc">.h
#import <UIKit/UIKit.h>

@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


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值