【问题标题】:Customize UITextField Clear Button Color to White Color [duplicate]将 UITextField 清除按钮颜色自定义为白色 [重复]
【发布时间】:2016-08-15 14:49:50
【问题描述】:

我想更改UITextField 的清除按钮的颜色,据我所知,我可以通过访问 TextField 的 RightView 属性来实现相同的效果,也可以使用图像来做同样的事情。

但是不,我只想更改那个按钮的颜色。

有人可以帮助我吗?

下面是我为访问 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]])
                {

                }
            }
        }
    }
}

【问题讨论】:

  • 请说明您要更改文本字段背景或按钮颜色的内容?
  • 我想改变那个按钮的颜色。
  • 你在用故事板吗?
  • 是的,我正在使用 StoryBoard。

标签: ios objective-c xcode uibutton uitextfield


【解决方案1】:

从你的所有回答中,我得出了这个结论,

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】:

    我是 iOS 应用开发的新手。但我会尝试。请参考以下代码。

    .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
    

    【讨论】:

      猜你喜欢
      • 2020-03-27
      • 1970-01-01
      • 1970-01-01
      • 2015-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多