https://blog.csdn.net/Nathan1987_/article/details/63278263
方法1
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
//繼承UITextField 重寫(xiě)方法- (CGRect)textRectForBounds:(CGRect)bounds {returnCGRectInset(bounds,2,1);}- (CGRect)editingRectForBounds:(CGRect)bounds {returnCGRectInset(bounds,2,1);
//如果文字尺寸不高,顯示區(qū)域足夠大,UITextField就可以正常顯示。//當(dāng)UITextField不能完全顯示漢字的時(shí)候,就會(huì)變成可滾動(dòng),文字就會(huì)低于中心線,點(diǎn)擊刪除按鈕的時(shí)候,看起來(lái)就會(huì)向下偏移。//使用NSLOG輸出UITextField的layoutSubviews方法,顯示UITextEditor的contentOffset發(fā)生了偏移。//因此重寫(xiě)layoutSuviews方法,在[super layoutSubview]之后重置UITextEditor的contentOffset.y 就可以了。- (void)layoutSubviews{? ? [superlayoutSubviews];for(UIScrollView*view inself.subviews)? {if([view isKindOfClass:[UIScrollViewclass]])? ? {CGPointoffset = view.contentOffset;if(offset.y!=0)? ? ? ? ? ? {? ? ? ? ? ? ? ? offset.y=0;? ? ? ? ? ? ? ? view.contentOffset= offset;? ? ? ? ? ? }break;? ? }}
參考了下面三個(gè)帖子?