首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在iOS 7中使用iOS 6风格的分段控件?

在iOS 7中使用iOS 6风格的分段控件?
EN

Stack Overflow用户
提问于 2013-09-20 00:19:31
回答 9查看 27.9K关注 0票数 15

是否可以将iOS 7设备上的分段控件显示为iOS 6版本的控件?

我们真的还没有准备好重新设计界面,新的平面控件与我们UI的其余部分并不协调。如果可能的话,现在最好还是保留iOS 6的风格。

为了清楚起见,我使用iOS 6.1Base SDK进行编译。我知道这是我的问题的“显而易见的”答案,但它不起作用。通过这样做,大多数其他UI元素将在iOS 6样式中显示,但像UIAlertViewUIActionSheet一样,UISegmentedControl不会。但是,与UIAlertViewUIActionSheet不同的是,UISegmentedControls不像是一个“系统”项;它们应该能够在iOS 6模式下显示。

编辑:我认为如果我最终在这篇文章中加入一张图片会很有帮助(可能从一开始就应该这样做)。然而,我提供的答案确实解决了这个问题。此外,回想起来,这款可能毕竟是iOS 6的风格,它只是显示错误,看起来像iOS 7的风格。

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2013-09-20 02:13:07

通过手动设置所有属性,我设法很好地解决了这个问题,但它并不是很完美。

这就是我最终要做的:

代码语言:javascript
复制
- (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];
}
票数 20
EN

Stack Overflow用户

发布于 2013-09-27 19:47:54

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

代码语言:javascript
复制
- (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];
    }
}
票数 13
EN

Stack Overflow用户

发布于 2013-09-24 05:51:15

今天我自己也遇到了这个问题。我正在更新的应用程序很旧,仍然使用xib文件,所以我不知道这在故事板上是否有效。正如其他人所建议的那样,您仍然需要使用iOS 6.1SDK,但仅有这一点是不够的。执行以下步骤后,我能够恢复原来的UISegmentedControl外观:

  1. 打开有问题的界面构建器文档
  2. 转到文件检查器(第一个检查器选项卡;有一个文档图标)
  3. 在“界面构建器文档”部分下,将“打开位置”更改为Xcode4.6

我确实认为这是一个错误,如果在代码中创建的UISegmentedControl实例没有解决方法,我也不会感到惊讶。我猜这在某种程度上与iOS 7中segmentedControlStyle属性的弃用有关(请参阅https://developer.apple.com/library/ios/documentation/uikit/reference/UISegmentedControl_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instp/UISegmentedControl/segmentedControlStyle)。

希望这能对外面的人有所帮助。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18900034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档