2011-12-07 4 views
1

私のUISegmentの外観に助けが必要です。UISegmentcontrolの出現で問題が発生する

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

私はviewDidLoadのときにIBActionを呼び出しました。

それがこの

enter image description here

を表示するはずはなく、それは外観上の問題ですが、それを修正するために...私は外見コードをコメントしたときに、今わからない私は知っている、このことを示し、それは最初のでしょう画像。

enter image description here

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

答えて

1

上記のコードは唯一UIControlStateNormalの外観を設定しているように見え、あなたもUIControlStateSelectedのための外観を設定する必要があります。

+0

いや、ちょうど色が違うだけです – Desmond

+0

しかし、私は他のセグメントをクリックすると正しく動作します – Desmond

2

1つのセグメントに色を付けるうまい方法で、私はそのようなものを探していました。 しかし、今、私はそれが「法的な」方法ですかしら...

で:

[[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のために?

+0

コメントはありがたいですが、私の1stアプリをやっています。お勧めしますか? – Desmond

+1

私はこの質問に対して私が与えた答えを使用することができます:http://stackoverflow.com/questions/8551510/removing-the-white-gradient-from-uisegmentedcontrol/8555552#8555552 – meronix

関連する問題