【问题标题】:How to change color of UISegmentedControl border in iOS7?如何在 iOS7 中更改 UISegmentedControl 边框的颜色?
【发布时间】:2013-10-11 08:32:43
【问题描述】:


如何在不改变文本颜色的情况下改变 iOS7 中分段控制器的边框颜色?


如果我可以保持段之间的线不变(即与文本颜色相同),那将是理想的,但如果边框颜色的变化意味着这条线的变化也可以。

另请注意,文本(以及段之间的线)具有使用
[segmCtrl setTintColor:choosenTintColor]

设置的颜色

【问题讨论】:

标签: ios cocoa-touch ios7 uisegmentedcontrol uiappearance


【解决方案1】:

链接的答案确实回答了您的问题,但您必须在字里行间阅读。这是一个更清晰的示例,用于更改应用程序中的所有分段控件样式:

// Sets the tint color which typically sets the color of the segment images, text, dividers,
// borders, and selected segment. A translucent version of this color is also used to tint a
// segment when it is pressed and transitioning to being selected, as shown on the first
// segment below.
[[UISegmentedControl appearance] setTintColor:[UIColor blackColor]];

// The attributes dictionary can specify the font, text color, text shadow color, and text
// shadow offset for the title in the text attributes dictionary
[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];

对于应用内的一个控件:

// Sets the tint color which typically sets the color of the segment images, text, dividers,
// borders, and selected segment. A translucent version of this color is also used to tint a
// segment when it is pressed and transitioning to being selected, as shown on the first
// segment below.
self.segControl.tintColor = [UIColor blackColor];

// The attributes dictionary can specify the font, text color, text shadow color, and text
// shadow offset for the title in the text attributes dictionary
[self.segControl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];

更多信息在这里: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/UIKitUICatalog/UISegmentedControl.html

【讨论】:

  • 感谢您抽出宝贵时间帮助我。不过我自己找到了一个解决方案,我认为它更好(更容易理解)。
  • 如果您只想更改所选标签的文本颜色,请使用UIControlStateSelected 而不是UIControlStateNormal
【解决方案2】:

所以我自己解决了这个问题。我的解决方案为分段控件的边框提供了另一种颜色,仅此而已。

为了只更改分段控件的边框颜色,我将另一个分段控件放在旧控件之上。我禁用了这个新的用户交互,并将所选片段的图像设置为nil

UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40)];
// Header view for my main view

UISegmentedControl *subCat = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Segm 1", @"Segm 2", @"Segm 3", @"Segm 4", nil]]; 
// The UISegmentedController which I want to change color for

[subCat setFrame:CGRectMake(5, 5, [UIScreen mainScreen].bounds.size.width - 10, 30)];
[subCat setSelectedSegmentIndex:0];

UISegmentedControl *bcg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@" ", @" ", @" ", @" ", nil]]; 
// The UISegmentedController I put on top of the other one

UIColor *subColor = [UIColor redColor];
[subCat setTintColor:subColor];
[bcg setFrame:CGRectMake(5, 5, [UIScreen mainScreen].bounds.size.width - 10, 30)];
[bcg setTintColor:[UIColor greenColor]];
[bcg setUserInteractionEnabled:NO];
[bcg setSelectedSegmentIndex:0];
[bcg setImage:nil forSegmentAtIndex:0]; // Removing highlight color


[header addSubview:subCat];
[header addSubview:bcg];

[[self view] addSubview:header];

【讨论】:

  • 你为什么不这样做呢? [[UISegmentedControl 外观] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor redColor] } forState:UIControlStateNormal];
  • 出色的解决方法。建议将背景索引设置为 -1,而不是将 image 设置为 nil。 "[bcg setSelectedSegmentIndex:-1];"
【解决方案3】:

我决定为所有项目添加 viewWillAppear mySegmentControl.selectedIndex。 因此,色调颜色出现在所有段中。 当然,选中所有项目后,再次选择你的默认项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-17
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    相关资源
    最近更新 更多