UISegmentedControl tintColor

Mih*_*bar 5 cocoa-touch uisegmentedcontrol ios ios8

我在让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];
}
Run Code Online (Sandbox Code Playgroud)

如何使UISegmentedControl使用绿色色调?

Mih*_*bar 7

我最终为所需的行为创建了一个类别.子视图结构如下所示:

UISegment
   UISegmentLabel
   UIImageView
UISegment
   UISegmentLabel
   UIImageView
Run Code Online (Sandbox Code Playgroud)

因此,所需效果需要两个循环(否则某些部分保持旧色调).

UISegmentedControl + TintColor.h

#import <UIKit/UIKit.h>

@interface UISegmentedControl (TintColor)

@end
Run Code Online (Sandbox Code Playgroud)

UISegmentedControl + 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;
        }
    }
}

@end
Run Code Online (Sandbox Code Playgroud)


Boi*_*ime 3

尝试这样的事情吗?

\n\n
for (UIView *subView in mySegmentedControl.subviews)\n{\n   [subView setTintColor: [UIColor greenColor]];\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

但实际上这似乎是 iOS 7 中的已知问题,不知道 iOS 8 中是否已修复。

\n\n

“您无法在 iOS 7 上自定义分段控件\xe2\x80\x99s 样式。分段控件只有一种样式”

\n\n

UIKit 用户界面目录

\n