UITextField的左右圖片,內容位置調整,解決輸入框輸入時上下抖動

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


發佈了28 篇原創文章 · 獲贊 50 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章