我在我的objective-c应用程序中有一个UIButton。我的按钮被修改了,添加了文本和图像,如下所示:
- (void)centerButtonAndImageWithSpacing:(CGFloat)spacing {
CGFloat insetAmount = spacing / 2.0;
self.imageEdgeInsets = UIEdgeInsetsMake(0, -insetAmount, 0, insetAmount);
self.titleEdgeInsets = UIEdgeInsetsMake(0, insetAmount, 0, -insetAmount);
self.contentEdgeInsets = UIEdgeInsetsMake(0, insetAmount, 0, insetAmount);
}
然后我用下面的代码添加图像:
[self.myButton setImage:[UIImage imageNamed:@"imageButton.png"] forState:UIControlStateNormal];
我想要更改图像的tintColor,但当我尝试更改它时,我的代码无法工作:
[self.myButton setTintColor:[UIColor redColor]];
我正在尝试使用此代码,但不起作用:
UIImage* img = [UIImage imageNamed:imageName];
icon.image = [img imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
icon.tintColor = [UIColor redColor];
我有这个:
我想要得到它:
如何更改imageButton的颜色?
发布于 2017-01-09 19:51:51
我们可以通过以下方式为透明图标应用所需的颜色。
1.将图像拖动到Assets.xcassets
上,并在资源属性检查器中将Render as属性更改为Template Image
。
2.按照下面的方式设置所需的色调颜色。
icon.tintColor = [UIColor redColor];
希望能对你有所帮助。
发布于 2017-01-10 16:11:40
更改tintColor属性的解决方案是使用renderingMode的setImage:
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate
使用以下代码:
[self.myButton setImage:[[UIImage imageNamed:@"myImageButton.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
然后将图像设置为按钮,使用以下代码我们可以正确地设置tintColor属性:
self.myButton.tintColor = [UIColor redColor];
发布于 2017-01-09 18:29:41
在界面生成器中以编程方式将UIButtonType更改为UIButtonTypeCustom
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
自行更改属性并重新创建圆角
myButton.backgroundColor = [UIColor redColor];
myButton.layer.borderColor = [UIColor blackColor].CGColor;
myButton.layer.borderWidth = 0.5f;
myButton.layer.cornerRadius = 10.0f;
让我知道状态..
https://stackoverflow.com/questions/41545599
复制相似问题