更新已回答。我是。
目前我UISegmentedControl
的文字颜色有问题;它需要在UIControlStateSelected
的第一次加载时更改。代码有效,但只是有条件的。当您在导航栏上使用分段控件访问页面,点击后退按钮,然后再次访问该页面时,它可以正常工作。我假设这里有继承问题。让我解释..
分段控件的位置位于导航栏的顶部。
包含SegmentedControl的ViewController的继承: TabBarViewController(使用AppDelegate管理) - >导航控制器 - > ViewController('inviteSegBar'所在的位置)
以下是AppDelegate.m中的代码:
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithHexString:@"#669900"]];//this one sets it green.
[[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
以下是VC的viewDidLoad:
代码,其中包含'inviteSegBar',问题为UISegmentedControl
:
- (void)viewDidLoad
{
[super viewDidLoad];
//CUSTOM APPEARANCE <below>
self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:@"#669900"];
inviteSegBar.tintColor = [UIColor colorWithHexString:@"#333333"];
[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#669900"]} forState:UIControlStateSelected];
}
就像我说的最后一行有效,但只有当你重新访问页面时。为什么会这样?
PS 这是同样的问题,我已经在列出任何答案之前尝试过此代码。
答案 0 :(得分:9)
找到答案:简单地移动
[[UISegmentedControl appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#669900"]} forState:UIControlStateSelected];
到您的AppDelegate.m文件
答案 1 :(得分:6)
使用
UIColor *whitecolor = [UIColor whiteColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:@[whitecolor] forKeys:@[UITextAttributeTextColor]];
[yourSegment setTitleTextAttributes:attributes
forState:UIControlStateNormal];
UIColor *grayColor = [UIColor darkGrayColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:@[grayColor] forKeys:@[UITextAttributeTextColor]];
[yourSegment setTitleTextAttributes:attributes
forState:UIControlStateSelected];
<强>更新强>
UIColor *whitecolor = [UIColor whiteColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:@[whitecolor] forKeys:@[NSForegroundColorAttributeName]];
[yourSegment setTitleTextAttributes:attributes
forState:UIControlStateNormal];
UIColor *grayColor = [UIColor darkGrayColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:@[grayColor] forKeys:@[NSForegroundColorAttributeName]];
[yourSegment setTitleTextAttributes:attributes
forState:UIControlStateSelected];
答案 2 :(得分:5)
此代码允许您在分段控件中为标签设置一些文本属性:
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], UITextAttributeTextColor,
nil];
[_segmentedControl setTitleTextAttributes:attributes forState:UIControlStateSelected];
Apple文档中允许的更多属性:link
答案 3 :(得分:2)
这可能会对您有所帮助:
UIAppearance代理设置标题文本属性但保留tintColor用于边框。
[[UISegmentedControl appearance] setTitleTextAttributes:@{
NSForegroundColorAttributeName : [UIColor redColor]
} forState:UIControlStateNormal];
答案 4 :(得分:1)
对于更改UISegmentedControl外观,例如插入到viewDidLoad函数中此代码:
// color selected text ---> red
[[UISegmentedControl appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor redColor] } forState:UIControlStateSelected];
// color disabled text ---> blue
[[UISegmentedControl appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blueColor] } forState:UIControlStateNormal];
// color tint segmented control ---> black
[[UISegmentedControl appearance] setTintColor:[UIColor greenColor]];