1

所以我为我的通用 UITextField 定制了一个 clearButton。我正在尝试移动 clearButton,因为我的 uitextfield 的背景在右侧有一些设计内容。我需要将清除按钮推到大约 150 像素以上。

我尝试了所有这些组合,但最终将清除按钮推离屏幕或使清除按钮非常宽,因此 uitextfield 中的文本停止变短。

第一次尝试

UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];

CGRect rect;
UIEdgeInsets contentInsets;
CGRect result;
[clearBtn setImage:[UIImage imageNamed:@"design.png"] forState:UIControlStateNormal];
rect = CGRectMake(0, 0, 45, 45);
contentInsets = UIEdgeInsetsMake(0, 50, 0, 0);
result = UIEdgeInsetsInsetRect(rect, contentInsets);
[clearBtn setFrame:result];

第二次尝试

UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];

CGRect rect;
UIEdgeInsets contentInsets;
CGRect result;
[clearBtn setImage:[UIImage imageNamed:@"design.png"] forState:UIControlStateNormal];
rect = CGRectMake(0, 0, 45, 45);
contentInsets = UIEdgeInsetsMake(0, 250, 0, 150);
result = UIEdgeInsetsInsetRect(rect, contentInsets);
[clearBtn setFrame:result];
4

1 回答 1

3

更好的选择可能是子类UITextField化并覆盖该clearButtonRectForBounds:方法。

- (CGRect)clearButtonRectForBounds:(CGRect)bounds {
    CGRect rect = [super clearButtonRectForBounds:bounds];
    rect.origin.x -= 150;

    return rect;
}

这会将清除按钮从其正常位置向左移动 150 点。

于 2014-08-04T23:26:44.337 回答