【问题标题】:Why does a UICollectionViewCell with a UIButton have a monochrome/tinted image?为什么带有 UIButton 的 UICollectionViewCell 具有单色/着色图像?
【发布时间】:2013-09-29 18:51:01
【问题描述】:

我创建了一个 UICollectionView 并添加了一个单元格,该单元格的唯一子视图是一个 UIButton。该按钮具有其标题和图像集。我已经在调试器中验证了图像数据是正确的。

当按钮在屏幕上绘制时,我看到了文本和图像,但是图像看起来好像已经填充了淡色,遮住了除形状以外的所有图像。

我在这里缺少什么让这个显示为普通按钮应该?

更新

事实证明,这不是特定于 UICollectionView,而是 iOS7 中的所有 UIButton。

iOS 7 使按钮中的所有图像都像模板图像一样使用图像的 alpha 通道与色调颜色相结合来生成图像(很像标签栏中的图像)。 UIImage 上有一个新的 renderingMode 属性,默认为“自动”,让上下文决定(这是按钮的模板样式)

这可以使用 UIImage 上的新 imageWithRenderingMode: 方法来规避:

UIImage* myImage = [UIImage imageNamed:@"Foo.png"];
myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[button setImage:myImage forState:UIControlStateNormal];

【问题讨论】:

    标签: ios objective-c uibutton uiimage ios7


    【解决方案1】:

    避免这种情况的最简单方法是使用不同的UIButtonType。 IOS 7 上的 UIButtonTypeSystem 具有此行为,因此您可以改用自定义按钮:

    UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
    [button setImage:myImage forState:UIControlStateNormal];
    

    【讨论】:

      【解决方案2】:

      当UIButton的背景色为lightText时,不会关闭按钮图片。

      UIButton.backgroundColor = UIColor.lightText
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-02
        • 1970-01-01
        • 1970-01-01
        • 2021-01-05
        • 1970-01-01
        • 1970-01-01
        • 2014-10-14
        • 2019-02-05
        相关资源
        最近更新 更多