寫輸入界面時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 );
}