我在让UISegmentedControl显示所需的色调时遇到了问题。
// AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// need red tint color in other views of the app
[[UIView appearance] setTintColor:[UIColor redColor]];
return YES;
}
// ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *items = @[@"Item 1", @"Item 2"];
UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:items];
// would like to have this control to have a green tint color
control.tintColor = [UIColor greenColor];
[self.view addSubview:control];
}如何使UISegmentedControl使用绿色色调?
发布于 2015-04-25 16:45:47
试试这样的东西?
for (UIView *subView in mySegmentedControl.subviews)
{
[subView setTintColor: [UIColor greenColor]];
}但实际上这似乎是iOS 7中的一个已知问题,我不知道它是否在iOS 8中得到了修复。
“不能在iOS 7上自定义分段控件的样式。分段控件只有一种样式”
发布于 2015-04-25 17:08:45
最后,我为所期望的行为创建了一个类别。子视图结构如下所示:
UISegment
UISegmentLabel
UIImageView
UISegment
UISegmentLabel
UIImageView因此,为了达到预期效果,需要两个循环(否则,某些部分将保持旧的色调)。
UISegmentedControl+TintColor.h
#import <UIKit/UIKit.h>
@interface UISegmentedControl (TintColor)
@endUISegmentedControl+TintColor.m
#import "UISegmentedControl+TintColor.h"
@implementation UISegmentedControl (TintColor)
- (void)setTintColor:(UIColor *)tintColor {
[super setTintColor:tintColor];
for (UIView *subview in self.subviews) {
subview.tintColor = tintColor;
for (UIView *subsubview in subview.subviews) {
subsubview.tintColor = tintColor;
}
}
}
@endhttps://stackoverflow.com/questions/29867443
复制相似问题