25

我的应用程序中有一个 UISegmentedControl。从 iOS7 GM 开始,我使用的图像在 iOS7 设备上运行时不会显示。还有其他人有这个问题吗?

这是它在 iOS6.1 和更早版本中的样子iOS6-ScreenShot

这是它在 iOS7 中的样子iOS7-ScreenShot

这是代码:

self.theSegmentedControl.frame = CGRectMake(self.theSegmentedControl.frame.origin.x, self.theSegmentedControl.frame.origin.y, 320, 35);
[self.theSegmentedControl setBackgroundImage:[UIImage imageNamed:@"img_toggleInactive"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.theSegmentedControl setImage:[UIImage imageNamed:@"btn_onceActive"] forSegmentAtIndex:0];
[self.theSegmentedControl setImage:[UIImage imageNamed:@"btn_recurringInactive"] forSegmentAtIndex:1];
[self.theSegmentedControl setImage:[UIImage imageNamed:@"btn_scheduledInactive"] forSegmentAtIndex:2];
[self.theSegmentedControl setDividerImage:[UIImage imageNamed:@"separator"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

有没有人找到解决方法?

4

4 回答 4

61

呜呼!这是解决方法:

//Add clear color to mask any bits of a selection state that the object might show around the images
self.theSegmentedControl.tintColor = [UIColor clearColor];

UIImage *onceActive;
UIImage *recurringActive;
UIImage *scheduledActive;
UIImage *separator;

//Setting imageWithRenderingMode: to imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal for iOS7 is key
if ([UIImage instancesRespondToSelector:@selector(imageWithRenderingMode:)]) {
    onceActive = [[UIImage imageNamed:@"btn_onceActive"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    recurringActive = [[UIImage imageNamed:@"btn_recurringInactive"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    scheduledActive = [[UIImage imageNamed:@"btn_scheduledInactive"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    separator = [[UIImage imageNamed:@"separator"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

}
else {
    onceActive = [UIImage imageNamed:@"btn_onceActive"];
    recurringActive = [UIImage imageNamed:@"btn_recurringInactive"];
    scheduledActive = [UIImage imageNamed:@"btn_scheduledInactive"];
    separator = [UIImage imageNamed:@"separator"];
}


[self.theSegmentedControl setImage:onceActive forSegmentAtIndex:0];
[self.theSegmentedControl setImage:recurringActive forSegmentAtIndex:1];
[self.theSegmentedControl setImage:scheduledActive forSegmentAtIndex:2];
[self.theSegmentedControl setDividerImage:separator forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
于 2013-09-11T04:44:08.440 回答
18

Xcode 6/iOS 8 的更新

现在您可以在 Interface builder 中执行此操作

只需在资产目录中添加图像文件并将其“渲染为”原始图像而不是默认设置

代码 5

新的 UISegmented 控件使用 tint color 使用模板模式为图像着色。您需要将这些图像渲染为原始图像而不是模板。

正如评论中所建议的那样:

UIImage* onceActive = [UIImage imageNamed:@"btn_onceActive"];
if (IOS_7_MACRO)
    onceActive = [onceActive imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[self.theSegmentedControl setImage:onceActive forSegmentAtIndex:0];
于 2013-09-20T11:30:35.000 回答
7

使用此代码在带有 xCode 5.0 的 iOS 7 中的分段控制上设置图像

if ([UIImage instancesRespondToSelector:@selector(imageWithRenderingMode:)]) {

    [segmentControl setImage:[[UIImage imageNamed:@"image.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:0];

}
else {
    [segmentControl setImage:[UIImage imageNamed:@"image.png"] forSegmentAtIndex:0];
}
于 2014-01-27T09:48:54.067 回答
2

创建一个类别可能很有用:

@interface UISegmentedControl (UISegmentedControlAdditions)
    -(void)setImageRenderingMode:(UIImageRenderingMode)renderingMode;
@end    

@implementation UISegmentedControl (UISegmentedControlAdditions)
    -(void)setImageRenderingMode:(UIImageRenderingMode)renderingMode {
        for (int index=0; index < [self numberOfSegments]; index++) {
            UIImage * image = [self imageForSegmentAtIndex:index];
            [self setImage:[image imageWithRenderingMode:renderingMode] forSegmentAtIndex:index];
        }
    }

...然后打电话

if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
    [colorSegmentedControl setImageRenderingMode:UIImageRenderingModeAlwaysOriginal];
于 2013-11-27T08:07:56.390 回答