博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
城市选择之搜索框
阅读量:6758 次
发布时间:2019-06-26

本文共 3772 字,大约阅读时间需要 12 分钟。

新项目要做城市选择,效果图出来,用系统的搜索框达不到效果图的效果,设置searchBarStyle属性时,UISearchBarStyleDefault时设置barTintColor后边框会有黑线,设置UISearchBarStyleMinimal时无黑线单文本框颜色不符合要求。所以就自定义了下。先看下效果图

上面两个效果图的区别就是下面的图有个城市和图片。所以就封装到一个类中。只是TextField的LeftView变化。还有个问题就是城市名长短不一,设置城市的时候要进行重绘,所以我在set城市名的时候调用重绘方法。具体代码如下:

////  SearchTextField.h//  LocationDemo////  Created by City--Online on 15/11/30.//  Copyright © 2015年 City--Online. All rights reserved.//#import 
typedef void(^LeftBtnClickBlock)();@interface SearchTextField : UITextField@property (nonatomic,strong) NSString *leftTitle;@property (nonatomic,copy) LeftBtnClickBlock btnClickBlock;-(instancetype)initNoLeftTitleWithFrame:(CGRect)frame;@end
////  SearchTextField.m//  LocationDemo////  Created by City--Online on 15/11/30.//  Copyright © 2015年 City--Online. All rights reserved.//#import "SearchTextField.h"#import "Global.h"@interface SearchTextField ()@property (nonatomic,strong) UIButton *leftBtn;@property (nonatomic,strong) UIImageView *leftImgView;@property (nonatomic,strong) UIImageView *searchImgView;@property (nonatomic,assign) BOOL isNoLeftTitle;@end@implementation SearchTextField-(instancetype)initNoLeftTitleWithFrame:(CGRect)frame{    _isNoLeftTitle=YES;    return [self initWithFrame:frame];}- (instancetype)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {                self.layer.cornerRadius=5;        self.layer.borderWidth=0.5;        self.layer.borderColor=XQBColorLineLight.CGColor;        self.leftViewMode=UITextFieldViewModeAlways;        self.clearButtonMode=UITextFieldViewModeWhileEditing;        self.backgroundColor=[UIColor whiteColor];                self.leftView=[[UIView alloc]init];                _leftBtn =[UIButton buttonWithType:UIButtonTypeSystem];        [_leftBtn addTarget:self action:@selector(btnClickHandle:) forControlEvents:UIControlEventTouchUpInside];        [_leftBtn setTitleColor:XQBColorTextMostLight forState:UIControlStateNormal];        [_leftBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];        _leftImgView=[[UIImageView alloc]init];        _leftImgView.image=[UIImage imageNamed:@"login_location_down.png"];        [_leftBtn addSubview:_leftImgView];                [self.leftView addSubview:_leftBtn];                _searchImgView=[[UIImageView alloc]init];        _searchImgView.image=[UIImage imageNamed:@"login_location_search.png"];        [self.leftView addSubview:_searchImgView];            }    return self;}-(void)layoutSubviews{    [super layoutSubviews];        if (_isNoLeftTitle) {        self.leftView.frame=CGRectMake(0, 0, 35, self.frame.size.height);        _leftBtn.frame=CGRectMake(0, 0, 0, 0);                _leftImgView.frame=CGRectMake(0, 0, 0, 0);        _searchImgView.frame=CGRectMake(10, (self.frame.size.height-15)/2, 15, 15);    }    else{        NSDictionary *attributes=@{NSFontAttributeName: XQBFontMiddle};        CGRect titleFrame=[_leftTitle boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];                        self.leftView.frame=CGRectMake(0, 0, 62+titleFrame.size.width, self.frame.size.height);                _leftBtn.frame=CGRectMake(10, 0, titleFrame.size.width+22, self.frame.size.height);                [_leftBtn setTitle:_leftTitle forState:UIControlStateNormal];                _leftImgView.frame=CGRectMake(titleFrame.size.width+10, (self.frame.size.height-5)/2, 10, 5);                _searchImgView.frame=CGRectMake(titleFrame.size.width+37, (self.frame.size.height-15)/2, 15, 15);    }    }-(void)btnClickHandle:(id)sender{    _btnClickBlock();}-(void)setLeftTitle:(NSString *)leftTitle{    _leftTitle=leftTitle;    _isNoLeftTitle=NO;    [self setNeedsLayout];}@end

 

转载地址:http://nzweo.baihongyu.com/

你可能感兴趣的文章
STL容器的效率比较
查看>>
Ubuntu 14.04 安装 DevStack与遇到的的问题记录
查看>>
Ckeditor使用总结
查看>>
Express详解
查看>>
BZOJ3938 & UOJ88:[集训队互测2015]Robot——题解
查看>>
15:开发Rsync服务启动脚本案例
查看>>
shiro 更改登录的用户名
查看>>
mysql 权限
查看>>
iOS的主要框架介绍
查看>>
继承与访问权限
查看>>
C++ While学习笔记(2)
查看>>
WPF Expander控件(扩展面板)
查看>>
Django 实现分库
查看>>
人事面试100问题--巧妙应答
查看>>
ES6学习笔记<三> 生成器函数与yield
查看>>
AR(增强现实) 知识篇
查看>>
BizTalk接收消息后路由失败
查看>>
【工具类】怎么进入阿里云docker仓库
查看>>
Ceres-Solver库入门
查看>>
悲惨而又丢人的创业经历:草根创业者含恨倾诉为什么失败
查看>>