更改UITextFields清除Button的色调颜色

时间:2018-01-15 05:03:15

标签: uitextfield ios11

我试图改变uitextfields清除按钮的颜色,如:

for (UIView *subView in self.urlTextField.subviews) {
    if ([subView isKindOfClass:[UIButton class]]) {
        UIButton *button = (UIButton *)subView;
        [button setImage:[[button imageForState:UIControlStateNormal] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
                forState:UIControlStateNormal];
        button.tintColor = [UIColor redColor];
    }
}

我可以通过这种方式添加自定义图像,但色彩颜色不会发生变化。

1 个答案:

答案 0 :(得分:0)

由于我无法找到改变原始图片色调的解决方案,我实施了此解决方法

-(void)setClearButtonColor: (UIColor *) color forTextfield: (UITextField *) textField{

    UIButton *clearButton = [textField valueForKey:@"_clearButton"];

    if (clearButton) {

        UIImage *image = [[UIImage imageNamed:@"textfieldClearButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        [clearButton setImage:image forState:UIControlStateNormal];
        [clearButton setTintColor: color];
    }
}

我创建了这个清晰的按钮图像(57x57)(@ 3x)

enter image description here

随意使用它。