UISegmentedControl tintColor
UISegmentedControl tintColor
我在让 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 使用绿色色调?
试试这样的东西?
for (UIView *subView in mySegmentedControl.subviews)
{
[subView setTintColor: [UIColor greenColor]];
}
不过貌似是iOS7中的已知问题,不知道在iOS8中有没有修复
"You cannot customize the segmented control’s style on iOS 7. Segmented controls only have one style"
我最终为所需的行为创建了一个类别。子视图结构如下所示:
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
我在让 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 使用绿色色调?
试试这样的东西?
for (UIView *subView in mySegmentedControl.subviews)
{
[subView setTintColor: [UIColor greenColor]];
}
不过貌似是iOS7中的已知问题,不知道在iOS8中有没有修复
"You cannot customize the segmented control’s style on iOS 7. Segmented controls only have one style"
我最终为所需的行为创建了一个类别。子视图结构如下所示:
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