我在让 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/