写输入界面时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 );
}