1

UISegmentの外観についてサポートが必要です。これをアプリのデリゲートに設定すると、すべて正常に機能します。

このコードを追加して選択したセグメントの色を変更するまで、問題が発生していました。

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];
    }
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」のみが宣言され、アップルはそれを使用して2つの異なる方法で色付けします要素、選択されたものと他のもの)。

だから、あなたの方法は本当にうまくいくかもしれません、そして私はそれを使うことを考えています...しかしそれが本当にプライベートセッター方法と考えられているならアップルはあなたのアプリを拒否するかもしれません...あなたはiStoreのために承認されたアプリでそれを使ったことがありますか?

于 2011-12-14T19:51:13.030 に答える
1

上記のコードはの外観を設定しているだけのようUIControlStateNormalです。また、の外観を設定する必要がありますUIControlStateSelected

于 2011-12-07T14:38:44.247 に答える