我需要帮助我的UISegment外观,我在我的app委托中设置一切正常。
直到我添加此代码来更改我选择的分段颜色,才会导致问题。
我在viewDidLoad时调用了IBAction。
它应该显示这个
但相反它显示了这一点,我知道是外观问题,但现在不确定修复它...当我评论外观代码时它将是第一张图片。
的appdelegate
//normal segment
[[UISegmentedControl appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Rokkitt" size:20.0],UITextAttributeFont,
[UIColor colorWithRed:75.0/255.0 green:75.0/255.0 blue:75.0/255.0 alpha:1.0], UITextAttributeTextColor,
[UIColor clearColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
nil] forState:UIControlStateNormal];
//selected segment
[[UISegmentedControl appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Rokkitt" size:20.0],UITextAttributeFont,
[UIColor whiteColor], UITextAttributeTextColor,
[UIColor clearColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
nil] forState:UIControlStateHighlighted];
IBAction电话
// Get number of segments
int numSegments = [infoSegment.subviews count];
// Reset segment's color (non selected color)
for( int i = 0; i < numSegments; i++ ) {
// reset color
[[infoSegment.subviews objectAtIndex:i] setTintColor:[UIColor colorWithRed:196.0/255.0 green:223.0/255.0 blue:155.0/255.0 alpha:1]];
}
// Sort segments from left to right
NSArray *sortedViews = [infoSegment.subviews sortedArrayUsingFunction:compareViewsByOrigin context:NULL];
// Change color of selected segment
[[sortedViews objectAtIndex:infoSegment.selectedSegmentIndex] setTintColor:[UIColor colorWithRed:51.0/255.0 green:166.0/255.0 blue:85.0/255.0 alpha:1]];
// Remove all original segments from the control
for (id view in infoSegment.subviews) {
[view removeFromSuperview];
}
// Append sorted and colored segments to the control
for (id view in sortedViews) {
[infoSegment addSubview:view];
}
答案 0 :(得分:2)
为单个片段着色的好方法,我正在寻找类似的东西。 但现在我想知道这是否是一种“合法”的方式......
使用:
[[infoSegment.subviews objectAtIndex:i] setTintColor:[UIColor colorWithRed:196.0/255.0 green:223.0/255.0 blue:155.0/255.0 alpha:1]];
看起来你正在使用UISegmentedControl中单个元素的“私有”属性“tintColor”,而不是苹果正式声明的(它被声明为整个UISegmentedControl的属性“tintColor”,然后苹果用它来着色2个不同的方式元素,选择的一个和另一个)。
所以,你的方法可以真正起作用,我正在考虑使用它...但如果它真的被认为是私有的setter方法,苹果可以拒绝你的应用程序...... 你有没有在批准用于iStore的应用中使用它?
答案 1 :(得分:1)
看起来上面的代码只是为UIControlStateNormal
设置外观,您还需要设置 UIControlStateSelected
的外观。