目标 C. 这个 UIButton 图像有啥问题?

Posted

技术标签:

【中文标题】目标 C. 这个 UIButton 图像有啥问题?【英文标题】:Objective C. What's wrong with this UIButton image?目标 C. 这个 UIButton 图像有什么问题? 【发布时间】:2014-10-22 19:31:37 【问题描述】:

我在 IB 中绘制了一组 UIButton。我想在每轮教育游戏的几个按钮上随机添加图像。

您可以在页面顶部看到一些图片:

您还可以看到,按钮中的图像是蓝色的。那么,我做错了什么(即为什么它们是蓝色的?)?它们是否被设置为背景图像?

我添加图片如下:

    int tmpTag0 = [[currentButtonArray objectAtIndex:0] integerValue];
    UIButton *tmpButton0 = (UIButton *)[self.view viewWithTag:tmpTag0];
    [tmpButton0 setImage:[UIImage imageNamed:temp1] forState:UIControlStateNormal];

........

    int tmpTag4 = [[currentButtonArray objectAtIndex:4] integerValue];
    UIButton *tmpButton4 = (UIButton *)[self.view viewWithTag:tmpTag4];
    [tmpButton4 setImage:[UIImage imageNamed:temp5] forState:UIControlStateNormal];

currentButtonArray 是一个包含 5 个随机按钮索引的数组,对应于 5 个按钮标签。

非常感谢您的任何想法。

我在 Xcode 帮助中查找 UIButton 时看到的内容:

更新: 在下面的人的帮助下,这对我有用:

int tmpTag0 = [[currentButtonArray objectAtIndex:0] integerValue];
UIButton *tmpButton0 = (UIButton *)[self.view viewWithTag:tmpTag0];
UIImage *buttonImage0 = [UIImage imageNamed:temp1];
buttonImage0 = [buttonImage0 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tmpButton0 setImage:buttonImage0 forState:UIControlStateNormal];

【问题讨论】:

请阅读 Xcode 文档中名为“Buttons”的文档。它解释了这一点。 我已经用我在 Xcode 文档中看到的内容标记了我的问题.. 即.. 不多。你能提供一个网址,或者给白痴的说明:) 如果您在文档中搜索“按钮”,您应该会在列表底部的 SDK 指南中看到名为“按钮外观”的内容。看看那个。 如果你被关在那个小小的手机里,你也会是蓝色的。 是的。我绝对是大码。 【参考方案1】:

我quote:

使用图像字段,您可以指定要显示在 按钮的内容。 ...请注意,此图像将自动 呈现为按钮内的模板图像,除非您明确表示 将其渲染模式设置为 UIImageRenderingModeAlwaysOriginal。

所以,大概:

UIImage *image = [[UIImage imageNamed:temp5] 
                   imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tmpButton4 setImage:image ...

(等)

【讨论】:

太棒了。谢谢..您能否指出您引用的文档? 哦,链接隐藏在“引用”这个词的下面——是这样的:developer.apple.com/library/ios/documentation/userexperience/…(向下滚动到“图片”标题) 谢谢汤米,在你的回答和文档之间——也谢谢@rdelmar——我明白了..我在我的问题中发布了结果..【参考方案2】:

在我看来,更优雅的选择可能是在界面生成器中转到UIButton 的属性检查器,然后在Type 下选择Custom。仅当您为按钮保留默认的 System 类型时,才会出现您所指的蓝色版本。您可以像往常一样通过代码或在界面构建器中初始化按钮。

【讨论】:

以上是关于目标 C. 这个 UIButton 图像有啥问题?的主要内容,如果未能解决你的问题,请参考以下文章

如何保持 UIButton 的图像颜色不会重置为默认值?

UIButton 图像从 URL 加载后淡入淡出

IOS SWIFT-基于uibutton图像的调用代码块

无法更改 UIButton 图像

长按手势在 UIButton 中显示图像

拖动 UIButton 时移动拖动手柄