UITextField的左右图片,内容位置调整,解决输入框输入时上下抖动

来源:互联网 发布:手机fps显示软件 编辑:程序博客网 时间:2024/04/28 21:01

写输入界面时UITextField左边一般都会插入一个图片,以前直接约束一个UIImageView上去,今天看到了苹果原来已经提供了属性(你们这些愚蠢的人类)。

@property(nullable, nonatomic,strong) UIView *leftView;//左视图

@property(nonatomic) UITextFieldViewMode leftViewMode; //设置时机UITextFieldViewModeAlways即可

@property(nullable, nonatomic,strong) UIView *rightView//右视图
@property(nonatomic) UITextFieldViewMode rightViewMode;//设置时机UITextFieldViewModeAlways即可


//可以重写以下方法来调整相应位置

- (CGRect)borderRectForBounds:(CGRect)bounds;//边框
- (CGRect)textRectForBounds:(CGRect)bounds;//文本输入完成时
- (CGRect)placeholderRectForBounds:(CGRect)bounds;//占位符
- (CGRect)editingRectForBounds:(CGRect)bounds;//文本输入时
- (CGRect)clearButtonRectForBounds:(CGRect)bounds;//清除按钮
- (CGRect)leftViewRectForBounds:(CGRect)bounds;//左视图

- (CGRect)rightViewRectForBounds:(CGRect)bounds; //右试图


 //解决输入时向下抖动的情况

- (CGRect)editingRectForBounds:(CGRect)bounds {
return CGRectInset( bounds , 1 , 0 );
}


3 0
原创粉丝点击