ios - UISegmentedControl tintColor

标签 ios cocoa-touch ios8 uisegmentedcontrol

我在让 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 使用绿色色调?

最佳答案

我最终为所需的行为创建了一个类别。 subview 结构如下所示:

UISegment
   UISegmentLabel
   UIImageView
UISegment
   UISegmentLabel
   UIImageView

因此需要两个循环才能达到所需的效果(否则某些部分会保留旧色调)。

UISegmentedControl+TintColor.h

#import <UIKit/UIKit.h>

@interface UISegmentedControl (TintColor)

@end

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

关于ios - UISegmentedControl tintColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29867443/

相关文章:

ios - "Find Call Hierarchy"无法在 Xcode 中工作(带有 cocoapods 的大项目)

ios - 显示动画并锁定当前 View

ios - 处理 iOS 8 键盘扩展中的返回键

iphone - 在 iPhone 项目中使用 BCTabBarController

c# - Monotouch 的 Pinterest SDK 绑定(bind) - CreatePinWithImageURL 崩溃

objective-c - Box2D动态体掉屏

iphone - 碰撞检测

ios - 在 iOS 8 上调用系统共享菜单

ios - MFMessageComposeViewController 栏透明

ios - 如何定义嵌入 ViewController 的 NavigationController?