首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UISegmentedControl tintColor

UISegmentedControl tintColor
EN

Stack Overflow用户
提问于 2015-04-25 15:57:07
回答 2查看 2K关注 0票数 5

我在让UISegmentedControl显示所需的色调时遇到了问题。

代码语言:javascript
复制
// 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使用绿色色调?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-25 16:45:47

试试这样的东西?

代码语言:javascript
复制
for (UIView *subView in mySegmentedControl.subviews)
{
   [subView setTintColor: [UIColor greenColor]];
}

但实际上这似乎是iOS 7中的一个已知问题,我不知道它是否在iOS 8中得到了修复。

“不能在iOS 7上自定义分段控件的样式。分段控件只有一种样式”

UIKit用户界面目录

票数 3
EN

Stack Overflow用户

发布于 2015-04-25 17:08:45

最后,我为所期望的行为创建了一个类别。子视图结构如下所示:

代码语言:javascript
复制
UISegment
   UISegmentLabel
   UIImageView
UISegment
   UISegmentLabel
   UIImageView

因此,为了达到预期效果,需要两个循环(否则,某些部分将保持旧的色调)。

UISegmentedControl+TintColor.h

代码语言:javascript
复制
#import <UIKit/UIKit.h>

@interface UISegmentedControl (TintColor)

@end

UISegmentedControl+TintColor.m

代码语言:javascript
复制
#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
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29867443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档