【问题标题】:Button background and gradient change since iOS6自 iOS6 以来的按钮背景和渐变变化
【发布时间】:2012-09-24 12:04:57
【问题描述】:

对于一个应用程序,我必须创建一个填充有渐变和背景图像的UIButton。一切正常,直到我将操作系统从 iOS 5.1 升级到最近发布的 iOS 6。

以下是模拟器的两张截图:

嗯,第一个屏幕截图显示了我需要(并且做了),你可以看到棕色背景和灰色辐射。

以下是具有相同按钮但运行 iOS 6 的屏幕截图。如您所见,渐变消失了,UIButton 的底部出现了一条奇怪的白色条带。

我查看了这是否是错误或什么,但我什么也没发现,也许这里有人遇到过同样的问题?这是我的渐变代码

CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = btnCountry.bounds;
    UIColor *colorTop = [UIColor colorWithRed:.992f green:.992f blue:.992f alpha:1];
    UIColor *colorBot = [UIColor colorWithRed:.788f green:.769f blue:.745f alpha:1];
    gradient.colors = [NSArray arrayWithObjects:(id)[colorTop CGColor], (id)[colorBot CGColor], nil];
    gradient.borderColor = [UIColor colorWithRed:.545f green:.506f blue:.459f alpha:1].CGColor;
    gradient.borderWidth = 1;

    gradient.masksToBounds = YES;
    gradient.cornerRadius = 11;
    [[btnCountry layer] insertSublayer:gradient atIndex:0];

【问题讨论】:

  • 我终于找到了一种方法,通过使用以下代码删除子视图来恢复按钮的“正常”外观:for(UIView* subView in btnCountry.subviews) if([subView isKindOfClass:NSClassFromString(@"UIGroupTableViewCellBackground")]) [subView removeFromSuperview]; 但现在,当我点击按钮时,它会导致错误 EXC_BAD_ACC,没有人线索还是什么?
  • 这个question是相关的

标签: iphone uibutton ios6 cagradientlayer


【解决方案1】:

这是 ios6 中一个非常奇怪的问题,我在设置渐变时遇到了同样的问题:

[myButton.layer insertSublayer:gradient atIndex:0];

所以我尝试将底线更改为此在 iOS 6 和较低版本的 IOS 中运行良好

[myButton.layer insertSublayer:gradient below:myButton.titleLabel.layer];

希望这会有所帮助

【讨论】:

    【解决方案2】:

    正如您在我的评论中看到的那样,问题来自 UIGroupTableViewCellBackground 类,我只是隐藏它。我认为这不是一个“干净”的解决方案,如果你有更好的解决方案我会很高兴听到它:-)

    代码如下:

    for(UIView* subView in btnCountry.subviews)
        if([subView isKindOfClass:NSClassFromString(@"UIGroupTableViewCellBackground")])
            [subView setHidden:YES];
    

    【讨论】:

      【解决方案3】:

      在索引位置 1 处插入图层对我有用。

      可能最好的解决方案是检查 iOS 版本,并根据它在索引 0 或 1 处插入。

      【讨论】:

        猜你喜欢
        • 2012-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-24
        • 1970-01-01
        • 2011-03-13
        • 1970-01-01
        相关资源
        最近更新 更多