是否可以对齐 UITextField 的clearButton?
我以编程方式将按钮添加到 textField:
_usernameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
但它的位置比文本字段的中心低一点。

我该如何解决?
是否可以对齐 UITextField 的clearButton?
我以编程方式将按钮添加到 textField:
_usernameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
但它的位置比文本字段的中心低一点。

我该如何解决?
您可以扩展UITextField和覆盖clearButtonRectForBounds:并提供自定义矩形。
最终我UITextField继承了函数并覆盖了函数clearButtonRectForBounds:。
。H
#import <UIKit/UIKit.h>
@interface TVUITextFieldWithClearButton : UITextField
@end
.m
#import "TVUITextFieldWithClearButton.h"
@implementation TVUITextFieldWithClearButton
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
- (void)awakeFromNib
{
    self.clearButtonMode = UITextFieldViewModeWhileEditing;
}
- (CGRect)clearButtonRectForBounds:(CGRect)bounds
{
    return CGRectMake(bounds.size.width/2-20 , bounds.origin.y-3, bounds.size.width, bounds.size.height);
}
@end
    您是否尝试过字段本身的 contentVerticalAlignment 属性?
usernameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;