objective-c - UISegmentedControl setImage : Bug in iOS7

标签 objective-c cocoa-touch uisegmentedcontrol ios7

我的应用程序中有一个 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];

有没有人找到解决方法?

最佳答案

哇哦!解决方法如下:

//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];

关于objective-c - UISegmentedControl setImage : Bug in iOS7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18730667/

相关文章:

Objective-C 在按键时将应用程序置于最前面

c# - Xamarin Monotouch UISplitViewController : How can I set or create the delegate for UISplitViewController?

ios - 水平 UISwipeGestureRecognizer 不适用于 Small UIImageView

ios - 自动布局高度等于 MAX(多个 View 高度)

iphone - 一个可以通过编程方式和在 IB 中实例化的 View Controller ?

ios - UISegmentedControl 没有触发 UIControlEventValueChanged

ios - 如何等到代码完成。 iOS

objective-c - 在 UILabel 或 TextView 中显示垂直小数

ios - UISegmentedControl 在触摸后恢复到原始帧

ipad - 嵌入在 UINavigationBar/Item 中的 UISegmentedControl