解决UITextField输入时文字往下偏移的问题

548 阅读1分钟

方法1

textField.clearButtonMode = UITextFieldViewModeWhileEditing;

方法2

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

- (CGRect)editingRectForBounds:(CGRect)bounds 
{
    return CGRectInset(bounds, 2, 1);
}

方法3

//如果文字尺寸不高,显示区域足够大,UITextField就可以正常显示。
//当UITextField不能完全显示汉字的时候,就会变成可滚动,文字就会低于中心线,点击删除按钮的时候,看起来就会向下偏移。
//使用NSLOG输出UITextField的layoutSubviews方法,显示UITextEditor的contentOffset发生了偏移。
//因此重写layoutSuviews方法,在[super layoutSubview]之后重置UITextEditor的contentOffset.y 就可以了。
- (void)layoutSubviews
{
    [super layoutSubviews];
    for (UIScrollView *view in self.subviews) 
  {
        if ([view isKindOfClass:[UIScrollView class]])
     {
            CGPoint offset = view.contentOffset;
            if (offset.y != 0) 
           {
                offset.y = 0;
                view.contentOffset = offset;
            }
            break;
    }
}

参考了下面三个帖子 帖子1--stackoverflow 帖子2--stackoverflow 帖子3--cocoachina