2016-04-22 71 views
3

我想更改UITextField的清除按鈕的顏色,據我所知我可以通過訪問TextField的RightView屬性來實現相同的功能以及我也可以使用Image照着做。自定義UITextField清除按鈕顏色爲白色

但不,我只想改變那個按鈕的顏色。

有人能幫助我嗎?

下面的代碼是我寫的用來訪問UITextField的清除按鈕的代碼。

for (UIView *aSubview in self.view.subviews) { 
    for (UIView *bSubview in aSubview.subviews) { 
     if ([bSubview isKindOfClass:[UITextField class]]){ 
      for(UIView *v in bSubview.subviews) 
      { 
       if([v isKindOfClass:[UIButton class]]) 
       { 

       } 
      } 
     } 
    } 
} 
+0

請說明你想改變什麼文本框背景或按鈕顏色? –

+0

我想改變那個按鈕的顏色。 –

+0

你在使用故事板嗎? –

回答

4

從你的答案,我得出這樣的結論,

UIButton *btnClear = [self.txtEmail valueForKey:@"_clearButton"]; 
UIImage *imageNormal = [btnClear imageForState:UIControlStateNormal]; 
UIGraphicsBeginImageContextWithOptions(imageNormal.size, NO, 0.0); 
CGContextRef context = UIGraphicsGetCurrentContext(); 

CGRect rect = (CGRect){ CGPointZero, imageNormal.size }; 
CGContextSetBlendMode(context, kCGBlendModeNormal); 
[imageNormal drawInRect:rect]; 

CGContextSetBlendMode(context, kCGBlendModeSourceIn); 
[[UIColor whiteColor] setFill]; 
CGContextFillRect(context, rect); 

UIImage *imageTinted = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
[btnClear setImage:imageTinted forState:UIControlStateNormal]; 
-2

試試這個代碼,它會爲你工作:如果您使用的故事板,你可以很容易地改變你的UIButton外觀 enter image description here

1

我在iOS應用開發是新

yourbutton.backgroundColor = [UIColor whiteColor]; 

但我會嘗試下面的代碼。

.h文件中:

#import <UIKit/UIKit.h> 


@interface TextFieldTint : UITextField 

-(void) setColorButtonClearHighlighted:(UIColor *)colorButtonClearHighlighted; 
-(void) setColorButtonClearNormal:(UIColor *)colorButtonClearNormal; 

@end 

.m文件:

#import "TextFieldTint.h" 

@interface TextFieldTint() 

@property (nonatomic,strong) UIColor *colorButtonClearHighlighted; 
@property (nonatomic,strong) UIColor *colorButtonClearNormal; 

@property (nonatomic,strong) UIImage *imageButtonClearHighlighted; 
@property (nonatomic,strong) UIImage *imageButtonClearNormal; 


@end 

@implementation TextFieldTint 


-(void) layoutSubviews 
{ 
    [super layoutSubviews]; 
    [self tintButtonClear]; 
} 

-(void) setColorButtonClearHighlighted:(UIColor *)colorButtonClearHighlighted 
{ 
    _colorButtonClearHighlighted = colorButtonClearHighlighted; 
} 

-(void) setColorButtonClearNormal:(UIColor *)colorButtonClearNormal 
{ 
    _colorButtonClearNormal = colorButtonClearNormal; 
} 

-(UIButton *) buttonClear 
{ 
    for(UIView *v in self.subviews) 
    { 
     if([v isKindOfClass:[UIButton class]]) 
     { 
      UIButton *buttonClear = (UIButton *) v; 
      return buttonClear; 
     } 
    } 
    return nil; 
} 



-(void) tintButtonClear 
{ 
    UIButton *buttonClear = [self buttonClear]; 

    if(self.colorButtonClearNormal && self.colorButtonClearHighlighted && buttonClear) 
    { 
     if(!self.imageButtonClearHighlighted) 
     { 
      UIImage *imageHighlighted = [buttonClear imageForState:UIControlStateHighlighted]; 
      self.imageButtonClearHighlighted = [[self class] imageWithImage:imageHighlighted 
                    tintColor:self.colorButtonClearHighlighted]; 
     } 
     if(!self.imageButtonClearNormal) 
     { 
      UIImage *imageNormal = [buttonClear imageForState:UIControlStateNormal]; 
      self.imageButtonClearNormal = [[self class] imageWithImage:imageNormal 
                  tintColor:self.colorButtonClearNormal]; 
     } 

     if(self.imageButtonClearHighlighted && self.imageButtonClearNormal) 
     { 
      [buttonClear setImage:self.imageButtonClearHighlighted forState:UIControlStateHighlighted]; 
      [buttonClear setImage:self.imageButtonClearNormal forState:UIControlStateNormal]; 
     } 
    } 
} 


+ (UIImage *) imageWithImage:(UIImage *)image tintColor:(UIColor *)tintColor 
{ 
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGRect rect = (CGRect){ CGPointZero, image.size }; 
    CGContextSetBlendMode(context, kCGBlendModeNormal); 
    [image drawInRect:rect]; 

    CGContextSetBlendMode(context, kCGBlendModeSourceIn); 
    [tintColor setFill]; 
    CGContextFillRect(context, rect); 

    UIImage *imageTinted = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return imageTinted; 
} 
@end