解决UITextField输入时文字往下偏移的问题 --总有一种

2018-06-27  本文已影响0人  大佬的世界我不懂

https://blog.csdn.net/Nathan1987_/article/details/63278263

方法1

textField.clearButtonMode = UITextFieldViewModeWhileEditing;

方法2

//继承UITextField 重写方法- (CGRect)textRectForBounds:(CGRect)bounds {returnCGRectInset(bounds,2,1);}- (CGRect)editingRectForBounds:(CGRect)bounds {returnCGRectInset(bounds,2,1);

方法3

//如果文字尺寸不高,显示区域足够大,UITextField就可以正常显示。//当UITextField不能完全显示汉字的时候,就会变成可滚动,文字就会低于中心线,点击删除按钮的时候,看起来就会向下偏移。//使用NSLOG输出UITextField的layoutSubviews方法,显示UITextEditor的contentOffset发生了偏移。//因此重写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;    }}

参考了下面三个帖子 

帖子1–stackoverflow 

帖子2–stackoverflow 

帖子3–cocoachina

上一篇下一篇

猜你喜欢

热点阅读