问题描述
我需要帮助我的UISegment外观,我在我的应用程序委托中设置一切正常。
i need help on my UISegment appearances, i set this in my app delegate everything works fine.
直到我添加此代码来更改我选择的分段颜色,它导致了一个问题。
till i add in this code to change my selected segment color, it caused an issues.
我在viewDidLoad时调用了IBAction。
i called the IBAction when viewDidLoad.
它应该显示这个
但相反它显示了这个,我知道是外观问题,但现在不确定修复它...当我评论外观代码时它将是第一张图片。
but instead it show this, i know is the appearance issues but not sure now to fix it...when i commented the appearances codes it will the first picture.
appdelegate
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电话
IBAction call
// 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];
}
推荐答案
看起来像上面的代码只设置 UIControlStateNormal
的外观,还需要设置 UIControlStateSelected
的外观。
It looks like the code above is only setting appearance for UIControlStateNormal
, you also need to set the appearance for UIControlStateSelected
.
这篇关于UISegment控件外观导致问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!