在iOS 7中使用iOS 6风格分段控制?

是否有可能在iOS 7设备上显示分段控件作为控件的iOS 6版本?

我们实际上还没有准备好重新devise界面,新的平面控件不会与我们的其他用户界面进行交互。 如果可能的话,现在最好保持iOS 6的风格。

为了澄清,我正在编译使用iOS 6.1的基本SDK。 我知道这是我的问题的“显而易见”的答案,但它不起作用。 大多数其他UI元素将通过这样做来显示iOS 6样式,但是像UIAlertViewUIActionSheetUISegmentedControl不会。 但是,与UIAlertViewUIActionSheetUISegmentedControls不像“系统”项目; 他们应该能够以iOS 6模式显示。

编辑:我认为这将是有益的,如果我最终包括一个这样的图片(可能应该从一开始就这样做)。 但是,我提供的答案确实解决了这个问题。 此外,回想起来,它看起来像是iOS 6的风格毕竟,它只是显示如此错误,它看起来像iOS 7风格。

在这里输入图像说明

我设法通过手动设置所有的属性来做好解决这个问题的工作,但并不完美。

这就是我最终做的事情:

 - (void)fixSegmentedControlForiOS7 { NSInteger deviceVersion = [[UIDevice currentDevice] systemVersion].integerValue; if(deviceVersion < 7) // If this is not an iOS 7 device, we do not need to perform these customizations. return; NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont boldSystemFontOfSize:12], UITextAttributeFont, [UIColor whiteColor], UITextAttributeTextColor, nil]; [self.segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal]; NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]; [self.segmentedControl setTitleTextAttributes:highlightedAttributes forState:UIControlStateHighlighted]; self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; self.segmentedControl.tintColor = [UIColor colorWithRed:49.0 / 256.0 green:148.0 / 256.0 blue:208.0 / 256.0 alpha:1]; } 

要修复使用InterfaceBuilder分配的图像,请使用以下代码:

 - (void)fixImagesOfSegmentedControlForiOS7 { NSInteger deviceVersion = [[UIDevice currentDevice] systemVersion].integerValue; if(deviceVersion < 7) // If this is not an iOS 7 device, we do not need to perform these customizations. return; for(int i=0;i<toSegmentedControl.numberOfSegments;i++) { UIImage* img = [toSegmentedControl imageForSegmentAtIndex:i]; UIImage* goodImg = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; // clone image with different rendering mode [toSegmentedControl setImage:goodImg forSegmentAtIndex:i]; } } 

我自己今天就遇到了这个问题。 我正在进行更新的应用程序是相当旧的,仍然使用xib文件,所以我不知道这是否可以在故​​事板上工作。 正如其他人上面提到的,你仍然需要使用iOS 6.1 SDK,但是这仅仅是不够的。 执行以下步骤后,我能够恢复旧的UISegmentedControl外观:

  1. 打开有问题的界面生成器文档
  2. 转到文件检查器(第一个检查器选项卡;有一个文件图标)
  3. 在“界面生成器文档”部分下,将“打开”改为Xcode 4.6

我相信这是一个错误,如果没有解决代码中创build的UISegmentedControl实例的问题,我不会感到惊讶。 我猜这与iOS 7中segmentedControlStyle属性的弃用有些相关(请参阅https://developer.apple.com/library/ios/documentation/uikit/reference/UISegmentedControl_Class/DeprecationAppendix/AppendixAprepreatedAPI.html#// apple_ref / occ / instp / UISegmentedControl / segmentedControlStyle )。

希望这可以帮助那里的人。

如果您保存了以前版本的XCode中的iPhoneOS6.1.sdk文件,并将其添加到Xcode 5的相同path中,则可以针对6.1 SDK构build一个应用程序,以便在7上运行时,一切都如同6。如果可能的话,iOS7 SDK告诉iOS使所有的东西看起来像iOS7。 基本上,你有一个iOS6的应用程序,但与XCode 5build设。

如果您在任何UISegmentedControl细分上使用图像,则需要添加一些代码才能在iOS 7上正确设置这些图像,否则将用作模板图像,而选定的细分将作为细分区域的背景。

除非另有说明,iOS 7下的UISegmentedControl将其图像解释为呈现模式UIImageRenderingModeAlwaysTemplate。 我不得不在iOS 7的每个片段的图像上使用[UIImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]来实现以前的行为。

你可以:

  • 直到您准备好进行一些用户界面更改,才会针对iOS7更新您的应用。 针对iOS6 SDK编译的应用程序将在iOS7上以iOS6兼容模式运行,并且看起来与在iOS6中完全相同。
  • 将自定义背景,分隔符等图像应用到模仿iOS6的外观的分段控件中。

可能吗? 不是真的…

您可以制作自己的自定义分段控件。

或者你可以使用UIAppearance代理来自定义你的图像的分段控制,但是这是你的责任,使它看起来像在iOS 6上。

是的,如果您自己重新创build控制,则可以。 创build一个假的分段控制,看起来像和一样工作。

在我的应用程序中,我已经将Segmented控件设置为“Bar”样式。 它呈现在我的ios7 iphone5(哇,5,6,7)上的ios6风格。 但是,无论视图有多宽,段中的文本都被剪切并添加了三个“…”点。 所以ios7中的ios6分段控制渲染看起来真的很麻烦