【问题标题】:Is it possible to allign clearButton of UITextField是否可以对齐 UITextField 的清除按钮
【发布时间】:2013-02-13 16:26:16
【问题描述】:
UITextField的clearButton可以对齐吗?
我以编程方式将按钮添加到 textField:
_usernameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
但它的位置比文本字段的中心低一点。
我该如何解决?
【问题讨论】:
标签:
iphone
ios
objective-c
uitextfield
【解决方案3】:
最终我继承了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