在 iOS 7 中使用 iOS 6 样式分段控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18900034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use iOS 6 Style Segmented Control in iOS 7?
提问by MikeS
Is it possible to have a segmented control on an iOS 7 device show up as the iOS 6 version of the control?
是否可以将 iOS 7 设备上的分段控件显示为控件的 iOS 6 版本?
We really aren't ready for an interface redesign and the new flat control doesn't jive with the rest of our UI. It would definitely be best to keep the iOS 6 style for now, if possible.
我们真的没有准备好重新设计界面,新的平面控件与我们 UI 的其余部分不协调。如果可能的话,现在最好保持 iOS 6 风格。
To clarify, I am compiling using the iOS 6.1 Base SDK. I am aware that this is the "obvious" answer to my question, but it does not work. Most other UI elements will show up with iOS 6 styling by doing this, but like the UIAlertView
and UIActionSheet
, the UISegmentedControl
does not. However, unlike the UIAlertView
and UIActionSheet
, UISegmentedControls
do not feel like a "system" item; they should be able to display in iOS 6 mode.
澄清一下,我正在使用 iOS 6.1 Base SDK 进行编译。我知道这是我问题的“明显”答案,但它不起作用。通过这样做,大多数其他 UI 元素将显示为 iOS 6 样式,但与UIAlertView
和 一样UIActionSheet
,UISegmentedControl
则不会。但是,与UIAlertView
and不同UIActionSheet
,UISegmentedControls
感觉不像是一个“系统”项;它们应该能够在 iOS 6 模式下显示。
Edit: I thought it would be helpful if I finally included a picture with this (probably should have done this from the start). However, the answer I provided did fix the issue. Also, in retrospect, it looks like this mightbe the iOS 6 style after all, it's just displaying so wrong that it appears like iOS 7 style.
编辑:我认为如果我最终包含一张图片会有所帮助(可能应该从一开始就这样做)。但是,我提供的答案确实解决了问题。此外,回想起来,这毕竟可能是 iOS 6 风格,它只是显示错误,看起来像 iOS 7 风格。
回答by MikeS
I manage to do a pretty good job of solving this problem by setting all the attributes manually, but it is not quite perfect.
通过手动设置所有属性,我设法很好地解决了这个问题,但它并不完美。
This is what I ended up doing:
这就是我最终做的:
- (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];
}
回答by cat
To fix images assigned with InterfaceBuilder use this code:
要修复使用 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];
}
}
回答by ianthetechie
I just ran into this problem today myself. The app I'm working on updating is quite old, and still uses xib files, so I do not know if this works on storyboards or not. As others suggested above, you still need to use the iOS 6.1 SDK, but this alone is not enough. After performing the following steps, I was able to get the old UISegmentedControl
appearance back:
我今天自己也遇到了这个问题。我正在更新的应用程序很旧,仍然使用 xib 文件,所以我不知道这是否适用于故事板。正如上面其他人所建议的,您仍然需要使用 iOS 6.1 SDK,但这还不够。执行以下步骤后,我能够恢复旧UISegmentedControl
外观:
- Open the interface builder document in question
- Go to the file inspector (first inspector tab; has a document icon)
- Under the "Interface Builder Document" section, change "Opens in" to Xcode 4.6
- 打开有问题的界面构建器文档
- 转到文件检查器(第一个检查器选项卡;有一个文档图标)
- 在“Interface Builder Document”部分下,将“Opens in”更改为Xcode 4.6
I do believe this is a bug, and I would not be surprised if there isn't a workaround for UISegmentedControl
instances created in code. I'm guessing this is somewhat related to the deprecation of the segmentedControlStyle
property in iOS 7 (see https://developer.apple.com/library/ios/documentation/uikit/reference/UISegmentedControl_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instp/UISegmentedControl/segmentedControlStyle).
我确实相信这是一个错误,如果UISegmentedControl
在代码中创建的实例没有解决方法,我不会感到惊讶。我猜这与segmentedControlStyle
iOS 7 中属性的弃用有些相关(请参阅https://developer.apple.com/library/ios/documentation/uikit/reference/UISegmentedControl_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref /occ/instp/UISegmentedControl/segmentedControlStyle)。
Hope this helps someone out there.
希望这可以帮助那里的人。
回答by ahwulf
If you save the iPhoneOS6.1.sdk file from the previous version of XCode and add it to Xcode 5 in the same path you can then build an app against the 6.1 SDK so that when it runs on 7 everything is like 6. Linking against iOS7 SDK tells iOS to make everything look like iOS7 if possible. Essentially then you have an iOS6 app but building it with XCode 5.
如果您从先前版本的 XCode 中保存 iPhoneOS6.1.sdk 文件并将其添加到 Xcode 5 的相同路径中,您就可以针对 6.1 SDK 构建一个应用程序,这样当它在 7 上运行时,一切都像 6 一样。 iOS7 SDK 告诉 iOS,如果可能的话,让一切看起来都像 iOS7。本质上,你有一个 iOS6 应用程序,但使用 XCode 5 构建它。
回答by Ryan
If you use images on any of your UISegmentedControl segments, you'll need to add some code to set those properly on iOS 7, otherwise they'll be used as a template image and the selected segment will be a cutout of the segment's background.
如果您在任何 UISegmentedControl 段上使用图像,则需要添加一些代码以在 iOS 7 上正确设置这些图像,否则它们将用作模板图像并且所选段将是段背景的剪切图。
UISegmentedControl under iOS 7 interprets its images as being in rendering mode UIImageRenderingModeAlwaysTemplate unless otherwise specified. I had to use -[UIImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] on each segment's image for iOS 7 to achieve the previous behavior.
除非另有说明,否则 iOS 7 下的 UISegmentedControl 将其图像解释为处于渲染模式 UIImageRenderingModeAlwaysTemplate。我不得不在 iOS 7 的每个段的图像上使用 -[UIImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] 来实现以前的行为。
回答by user1169629
In my app, I have set the Segmented control to "Bar" style. It renders in ios6 style on my ios7 iphone5 (whoa, 5,6,7). However, the text inside the segments are cut and have the three dots "..." added, no matter how wide the view is. So the ios6 segmented control rendering in ios7 seems really buggy
在我的应用程序中,我已将 Segmented 控件设置为“Bar”样式。它在我的 ios7 iphone5(哇,5,6,7)上以 ios6 风格呈现。但是,无论视图有多宽,段内的文本都会被剪切并添加三个点“...”。所以ios7中的ios6分段控件渲染好像真的有bug
回答by Jonathan Arbogast
Either you could:
要么你可以:
- Not update your app at all for iOS7 until you're ready to make some UI changes. Apps compiled against the iOS6 SDK will run in iOS6 Compatibility mode on iOS7 and will look exactly the same as they did in iOS6.
- Apply custom background, separator, etc images to your segmented controls that mimic the look they had in iOS6.
- 在您准备好进行一些 UI 更改之前,根本不要为 iOS7 更新您的应用程序。针对 iOS6 SDK 编译的应用程序将在 iOS7 上以 iOS6 兼容模式运行,并且看起来与在 iOS6 中完全相同。
- 将自定义背景、分隔符等图像应用于模仿它们在 iOS6 中的外观的分段控件。
回答by Daniel
Is it possible? Not really...
是否可以?并不真地...
You could make your own custom segmented control.
您可以制作自己的自定义分段控件。
Or you could use the UIAppearance
proxy to customise your segmented control with images but then it's your responsibility to make it look like it was on iOS 6.
或者您可以使用UIAppearance
代理来自定义带有图像的分段控件,但您有责任让它看起来像在 iOS 6 上。
回答by DuckDucking
Yes, it is possible if you recreate the control by your own. Create a fake segmented control that looks like and work like one.
是的,如果您自己重新创建控件是可能的。创建一个看起来和工作一样的假分段控件。