我想更改在UItextField上写入时显示的clearButton的颜色。有什么建议吗?我不是高级界面开发人员,我不知道该怎么做。
答案 0 :(得分:21)
你应该创建自己的UIButton(带图像,你需要一个你想要的按钮),实例化它并将这个按钮放在你的UITextField的rightView中。
CGFloat myWidth = 26.0f;
CGFloat myHeight = 30.0f;
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, myWidth, myHeight)];
[myButton setImage:[UIImage imageNamed:@"myButtonImage"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:@"myButtonImage"] forState:UIControlStateHighlighted];
[myButton addTarget:self action:@selector(doClear:) forControlEvents:UIControlEventTouchUpInside];
myTextField.rightView = myButton;
myTextField.rightViewMode = UITextFieldViewModeUnlessEditing;
您可能需要使用(这些是示例值)来优化对齐:
myButton.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
然后,您需要实现方法 - (void)doClear(id)sender;这将清除你的文本域。
答案 1 :(得分:1)
我用了乔治的答案,这就是我的目的:
let height = textField.bounds.height / 3
let width = height + 10
let rect = CGRect(x: 0, y: 0, width: width, height: height)
let myButton = UIButton(frame: rect)
myButton.setImage(UIImage(named: "clear button white"), for: .normal)
textField.rightView = myButton
textField.rightViewMode = .always
myButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
答案 2 :(得分:0)
我建议使用自定义图片并忽略更改原生UITextField的clearButton,因为Apple可能会更改子视图,当苹果更新iOS版本时,您的应用会崩溃。
我写了一个类别可以轻松添加图片或自定义按钮。
#import <UIKit/UIKit.h>
@interface UITextField (ClearButton)
/**
* @brief Initialize Custon clear button
*/
-(void)initialCustomClearButton;
-(void)setRightMarginDistance:(CGFloat )tempRightMargin;
// you can only use this methods
-(void)setClearButtonImage:(UIImage *)tempClearButtonImage;
-(void)setClearButton:(UIButton *)tempClearButton;
#pragma mark -
-(void)editTextField:(id)sender;
@end
#import "UITextField+ClearButton.h"
#include <objc/runtime.h>
#pragma makr - Dynamic Property
////////////////////////////////
#define Dynamic_Property( ObjectType , property , setProperty )\
Dynamic_Property_Getter( ObjectType , property)\
Dynamic_Property_Setter( ObjectType , property , setProperty , OBJC_ASSOCIATION_RETAIN_NONATOMIC )
//////// Getter ////////
#define Dynamic_Property_Getter( ObjectType , property )\
-(ObjectType)property\
{\
return objc_getAssociatedObject( self, @selector(property) );\
}
//////// Setter ////////
#define Dynamic_Property_Setter( ObjectType , property , setProperty , associationFlag) \
-(void)setProperty(ObjectType)property\
{\
objc_setAssociatedObject(self, @selector(property), property, associationFlag);\
}
////////////////////////////////
static CGFloat const kXSGTextFieldClearButtonRightMargin = 6.0f;
@implementation UITextField (ClearButton)
Dynamic_Property(UIButton*,clearBtn,setClearBtn:)
Dynamic_Property(UIImage*, clearButtonImg, setClearButtonImg:)
Dynamic_Property(UIImageView*, clearButtonImageView, setClearButtonImageView:)
Dynamic_Property(NSNumber*, isShow, setIsShow:)
Dynamic_Property(NSNumber*, customClearButtonMode, setCustomClearButtonMode:)
Dynamic_Property(NSNumber*, rightMargin , setRightMargin:)
-(void)initialCustomClearButton{
self.isShow = @NO;// 目前沒用到
self.clearButtonImageView = nil;
// 設定 Clear Button Mode
self.customClearButtonMode = @(self.clearButtonMode);
self.rightMargin = @(kXSGTextFieldClearButtonRightMargin);
self.clearButtonMode = UITextFieldViewModeNever;
[self resetClearButton];
[self addTarget:self action:@selector(editTextField:) forControlEvents:(UIControlEventEditingChanged)];
}
-(void)setRightMarginDistance:(CGFloat )tempRightMargin{
if( tempRightMargin < 0 ){
NSLog(@"Can't set right margin negative number !!");
}
else{
self.rightMargin = @(tempRightMargin);
[self resetClearButton];
}
}
-(void)setClearButtonImage:(UIImage *)tempClearButtonImage{
if( tempClearButtonImage == nil ){
NSLog(@"Can't set Clear Button Image to nil !!");
}
else{
self.clearButtonImg = tempClearButtonImage;
if( self.clearButtonImageView == nil ){
self.clearButtonImageView = [[UIImageView alloc] initWithImage:self.clearButtonImg];
}
else{
[self.clearButtonImageView setImage:self.clearButtonImg];
}
// 調整位置
[self resetImagePosition];
[self.clearBtn addSubview:self.clearButtonImageView];
[self editTextField:self];
}
}
-(void)setClearButton:(UIButton *)tempClearButton{
if( self.clearBtn != nil ){
[self.clearBtn removeTarget:self action:@selector(pressedClearBtn:) forControlEvents:(UIControlEventTouchUpInside)];
}
self.clearBtn = tempClearButton;
[self.clearBtn addSubview:self.clearButtonImageView];
// 設定 frame 調整位置
[self resetClearButton];
[self resetImagePosition];
}
#pragma mark - Private
-(BOOL)checkTextField{
BOOL isOK = YES;
if( self.text == nil || [self.text isEqualToString:@""] ){
isOK = NO;
}
return isOK;
}
-(void)resetClearButton{
if( self.clearBtn == nil ){
self.clearBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width - self.frame.size.height - [self.rightMargin floatValue],
0,
self.frame.size.height,
self.frame.size.height)];
}
else{
[self.clearBtn setFrame:CGRectMake(self.frame.size.width - self.clearBtn.frame.size.width - [self.rightMargin floatValue],
0,
self.clearBtn.frame.size.width,
self.clearBtn.frame.size.height)];
[self.clearBtn removeTarget:self action:@selector(pressedClearBtn:) forControlEvents:(UIControlEventTouchUpInside)];
}
[self.clearBtn addTarget:self action:@selector(pressedClearBtn:) forControlEvents:(UIControlEventTouchUpInside)];
self.rightView = self.clearBtn;
self.rightViewMode = UITextFieldViewModeAlways;
[self editTextField:self];
}
-(void)resetImagePosition{
// 調整位置
[self.clearButtonImageView setFrame:CGRectMake((self.clearBtn.frame.size.width - self.clearButtonImg.size.width)*0.5,
(self.clearBtn.frame.size.height - self.clearButtonImg.size.height)*0.5,
self.clearButtonImg.size.width,
self.clearButtonImg.size.height)];
}
-(void)pressedClearBtn:(id)sender{
self.text = @"";
// 隱藏圖片、 disable button
[self editTextField:self];
}
#pragma mark - 加入監聽
-(void)editTextField:(id)sender{
if( sender == self ){
if( [self checkTextField] ){
// 顯示圖片、 enable button
[self.clearBtn setEnabled:YES];
[self.clearBtn setHidden:NO];
[self.clearButtonImageView setHidden:NO];
self.isShow = @YES;
}
else{
// 隱藏圖片、 disable button
[self.clearBtn setEnabled:NO];
[self.clearBtn setHidden:YES];
[self.clearButtonImageView setHidden:YES];
self.isShow = @NO;
}
}
}
@end
UITextField *_textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 320 , 40)];
[_textField initialCustomClearButton];
// [UIImage imageNamed:@"clear"] is an image of your clear button.
[_textField setClearButtonImage:[UIImage imageNamed:@"clear"]];
和完成!
PS:详情在此处:UITextField+ClearButton.h和UITextField+ClearButton.m