1

我需要关于 UISegment 外观的帮助,我在我的应用程序委托中设置了这个,一切正常。

直到我添加此代码以更改我选择的段颜色,它导致了一个问题。

我在 viewDidLoad 时调用了 IBAction。

它应该显示这个

在此处输入图像描述

但相反它显示了这一点,我知道是外观问题,但现在不确定要修复它...当我评论外观代码时,它将是第一张图片。

在此处输入图像描述

应用委托

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

IBAaction 调用

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

2 回答 2

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”,然后苹果用它以两种不同的方式着色元素,选定的一个和另一个)。

所以,你的方法真的可以工作,我正在考虑使用它......但如果它真的被认为是私有设置方法,苹果可能会拒绝你的应用程序......你曾经在批准用于 iStore 的应用程序中使用它吗?

于 2011-12-14T19:51:13.030 回答
1

看起来上面的代码只是设置外观UIControlStateNormal,您还需要设置外观UIControlStateSelected

于 2011-12-07T14:38:44.247 回答