2011-12-07 88 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

很好的方式來爲單個色塊着色,我正在尋找類似的東西。 但現在我不知道這是一個「合法」方式...

有:

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

看來你正在使用的「私有」財產「tintColor」在UISegmentedControl單一的元素,不正式由蘋果聲明(它被聲明爲整個UISegmentedControl的屬性「tintColor」,然後蘋果用它以兩種不同的方式着色元素,選定的和另一種)。

所以,你的方法可以真正的工作,我正在考慮使用它...但是蘋果可能會拒絕你的應用程序,如果它真的被認爲是私人設置方法... 你有沒有在一個應用程序批准使用它對於iStore?

+0

meronix感謝您的意見,但不太確定,現在做我的第一個應用程序....什麼將你推薦? – Desmond

+1

您可以使用我給出的這個問題的答案:http://stackoverflow.com/questions/8551510/removing-the-white-gradient-from-uisegmentedcontrol/8555552#8555552 – meronix