通过UITextField自定义UIsearchBar

学习IOS也快3个月了,开始学习的时候朋友就叫我写博客,那时候觉得没什么价值,现在学了三个月,在新公司一个多月多多少少也做了一点东西,从今天开始记录IOS开发。

好了废话就这么多咱入正题:

先说说UISearchBar公司在做一个综合性的项目,我昨天的任务就是写一个SearchBar,哈哈,是不是太小儿科了,nono,uisearchbar可真让我蛋疼了一个上午。本来公司有一个写好的模板,不过不符合规范,就是要做类似于这样的一个搜索框。首先,uisearchbar自定义太少,都写死了不好修改,当然也有一些可改的地方科室改来该去如何将绿色的搜索图标放在右边,且placehoder的内容放在左边很麻烦,很蛋疼,在stackflow里找了一上午,最简单的方法是加空格,给跪了有没有!!!我不想介绍太多的uisearchbar,因为个人觉得以后应该都不会用到uisearchbar了,。自定义的这个基本上实现了uiseaerchbar的所有功能,且可定制性更好。。。。

好了,想把一个uitextfield变成一个uisearchbar,首先要熟悉uitextfield 啊,怎么办呢,百度搜索吧

我参考了一边文章,文章地址:http://justsee.iteye.com/blog/1821461还是很详细的;


#define zdwidth self.view.frame.size.width
#define  zdheight self.view.bounds.size.height
@interface ViewController ()

@property (nonatomic, retain) UISearchBar *searchBar;
@property (nonatomic ,retain)UITextField *tySearchBar;
@property (nonatomic, retain) UIView *topSearchView;
@property (nonatomic, assign)BOOL isKeyBoardVisiable;
@property (nonatomic, retain) UIButton *searchBackgroundView;
@end

@implementation ViewController

- (id)init
{
    self = [super init];
    if (self)
    {
        _isKeyBoardVisiable = NO;
    }
    return self;
}
-(UIButton *)searchBackgroundView
{
    if(!_searchBackgroundView)
    {
        _searchBackgroundView = [[UIButton alloc] initWithFrame:CGRectMake(0 , 20, zdwidth, zdheight)];
        [_searchBackgroundView setBackgroundColor:[UIColor clearColor]];
        _searchBackgroundView.userInteractionEnabled = YES;
        [_searchBackgroundView addTarget:self action:@selector(SearchBtnClick:) forControlEvents:UIControlEventTouchDown];
        [self.view addSubview:_searchBackgroundView];
    }
    
    return _searchBackgroundView;
}
- (UIView *)topSearchView
{
    if (!_topSearchView)
    {
        _topSearchView = [[UIView alloc] init];
        _topSearchView.frame = CGRectMake(0, 20, self.view.frame.size.width, 44);
        _topSearchView.backgroundColor = [UIColor colorWithRed:236.f/255.f green:236.f/255.f blue:236.f/255.f alpha:1];
        [self.view addSubview:_topSearchView];
    }
    return _topSearchView;
}
- (UITextField *)tySearchBar
{
    if(!_tySearchBar)
    {
        _tySearchBar = [[UITextField alloc] initWithFrame:CGRectMake(10.0f, 27.0f, zdwidth-20, 30.0f)];
        UIFont *font = [UIFont fontWithName:@"Palatino-Roman" size:12.0];
        _tySearchBar.placeholder = @" 请输入搜索内容:";
        _tySearchBar.font = font;
        _tySearchBar.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
        _tySearchBar.autocorrectionType = UITextAutocorrectionTypeNo;
        _tySearchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _tySearchBar.keyboardType = UIKeyboardTypeDefault;
        _tySearchBar.backgroundColor=[UIColor clearColor];
        _tySearchBar.delegate = self;
        _tySearchBar.clearsOnBeginEditing = YES;
        _tySearchBar.clearButtonMode = UITextFieldViewModeAlways;
        _tySearchBar.returnKeyType =UIReturnKeySearch;
        _tySearchBar.backgroundColor = [UIColor whiteColor];
        
        UIImageView *rightView=[[UIImageView alloc]init];
        rightView.image= [UIImage imageNamed:@"tySearchBarIcon.png"];
        rightView.frame = CGRectMake(0, 0, 25, 25);
        _tySearchBar.rightView=rightView;
        _tySearchBar.rightViewMode=UITextFieldViewModeUnlessEditing;

        [self.view addSubview:_tySearchBar];
        
    }
    
    return _tySearchBar;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    //textField
    [self searchBackgroundView];
    [self topSearchView];
    [self tySearchBar];
   
    

}


#pragma mark
#pragma UITextFieldDelegate 显示键盘 撤销键盘等等
- (void)SearchBtnClick:(UIButton *)sender
{
    if(_isKeyBoardVisiable){
        self.tySearchBar.text = @"";
<span style="white-space:pre">	</span>//这里text为空 就会显示placehoder内容,
        [self.tySearchBar resignFirstResponder];
        _isKeyBoardVisiable= NO;
         NSLog(@"Searching ------------------------");
    }
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    
    return YES;
}// return NO to disallow editing.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    textField.keyboardAppearance=UIKeyboardAppearanceDefault;
    [textField becomeFirstResponder];
    _isKeyBoardVisiable = YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    return YES;
    
}// return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    
    self.tySearchBar.text = @"";
    [textField resignFirstResponder];
    _isKeyBoardVisiable = NO;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    self.tySearchBar.text = @"";
    [textField resignFirstResponder];
    _isKeyBoardVisiable = NO;
    NSLog(@"Searching ------------------------");
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

最后实现的界面就是这样子的:


最后样式,view层级,因为参考的那篇文章已经写的很详细了,就不做过多解释了,等会传demo

demo地址http://download.csdn.net/detail/da_sir/8274139

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值