是否可以对齐UITextField的清除按钮

时间:2013-02-13 16:26:16

标签: iphone ios objective-c uitextfield

是否可以将UITextField的 clearButton 对齐?

我已经以编程方式将按钮添加到textField:

_usernameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;

但它的位置比文本字段的中心稍低。

enter image description here

我该如何解决?

3 个答案:

答案 0 :(得分:1)

您可以展开UITextField并覆盖clearButtonRectForBounds:并提供自定义矩形。

答案 1 :(得分:0)

您是否尝试过该字段本身的contentVerticalAlignment属性?

usernameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

最初引用:Why doesn't the clear button aligned with the text in a UITextField?

答案 2 :(得分:0)

最终我将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
相关问题