UISegmentcontrol appearances causing issues

浪尽此生 提交于 2019-12-20 04:52:37

问题


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.

i called the IBAction when viewDidLoad.

it supposed to show this

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

   //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 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];
    }

回答1:


It looks like the code above is only setting appearance for UIControlStateNormal, you also need to set the appearance for UIControlStateSelected.




回答2:


nice way to tint the single segments, i was looking for something like that. But now i wonder if it's a "legal" way...

with:

[[infoSegment.subviews objectAtIndex:i] setTintColor:[UIColor colorWithRed:196.0/255.0 green:223.0/255.0 blue:155.0/255.0 alpha:1]];

it seems you are using the "private" property "tintColor" of the single elements in the UISegmentedControl, not officially declared by apple (it's declared just the property "tintColor" of the whole UISegmentedControl, then apple use it to colorize in 2 different way the elements, the selected one and the other).

so, your method could really work, and i'm considering to use it... but apple could reject your app if it's really considered a private setter method... have you ever used it in an app approved for iStore?



来源:https://stackoverflow.com/questions/8415922/uisegmentcontrol-appearances-causing-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!