2012-11-23 55 views
2

下面是我在我的應用程序中使用的開/關顏色自定義分段控件的代碼。但是,在iOS 6中,視圖首次加載時是默認顏色。一旦選擇了其中一個分段,就會顯示正確的自定義顏色。什麼我需要做的,讓它出現在視圖加載時,並確保它的設備兼容,而無法在iOS 6iOS 6 Segemented控件自定義色調顏色

#define kTagFirst 111 
#define kTagSecond 112 
#define kTagThird 113 

- (IBAction)segmentedControlChanged:(id)sender 
{ 
    UIColor *darkBG = [UIColor colorWithRed:149.0/255.0 green:133.0/255.0 blue:115.0/255.0 alpha:1.0]; 
    UIColor *lightBG = [UIColor colorWithRed:234.0/255.0 green:232.0/255.0 blue:217.0/255.0 alpha:1.0]; 
    UIColor *darkText = [UIColor colorWithRed:85.0/255.0 green:72.0/255.0 blue:60.0/255.0 alpha:1.0]; 
    UIColor *lightText = [UIColor whiteColor]; 

    [segmentedControl setTintColor:darkBG forTag:kTagFirst]; 
    [segmentedControl setTintColor:darkBG forTag:kTagSecond]; 
    [segmentedControl setTintColor:darkBG forTag:kTagThird]; 
// [segmentedControl setTextColor:lightText forTag:kTagFirst]; 
// [segmentedControl setTextColor:lightText forTag:kTagSecond]; 
// [segmentedControl setTextColor:lightText forTag:kTagThird]; 

    switch (segmentedControl.selectedSegmentIndex) 
    { 
    case 0: 
     mapView.hidden = YES; 
     infoView.hidden = YES; 
     [segmentedControl setTintColor:darkBG forTag:kTagFirst]; 
     [segmentedControl setTintColor:lightBG forTag:kTagSecond]; 
     [segmentedControl setTintColor:lightBG forTag:kTagThird]; 
     [segmentedControl setTextColor:lightText forTag:kTagFirst]; 
     [segmentedControl setTextColor:darkText forTag:kTagSecond]; 
     [segmentedControl setTextColor:darkText forTag:kTagThird]; 
     //[segmentedControl setShadowColor:lightBG forTag:kTagFirst]; 
     [segmentedControl setShadowColor:lightBG forTag:kTagSecond]; 
     [segmentedControl setShadowColor:lightBG forTag:kTagThird]; 
     break; 
    case 1: 
     mapView.hidden = NO; 
     infoView.hidden = YES; 
     [segmentedControl setTintColor:lightBG forTag:kTagFirst]; 
     [segmentedControl setTintColor:darkBG forTag:kTagSecond]; 
     [segmentedControl setTintColor:lightBG forTag:kTagThird]; 
     [segmentedControl setTextColor:darkText forTag:kTagFirst]; 
     [segmentedControl setTextColor:lightText forTag:kTagSecond]; 
     [segmentedControl setTextColor:darkText forTag:kTagThird]; 
     [segmentedControl setShadowColor:lightBG forTag:kTagFirst]; 
     //[segmentedControl setShadowColor:lightBG forTag:kTagSecond]; 
     [segmentedControl setShadowColor:lightBG forTag:kTagThird]; 
     break; 
    case 2: 
     mapView.hidden = YES; 
     infoView.hidden = NO; 
     [segmentedControl setTintColor:lightBG forTag:kTagFirst]; 
     [segmentedControl setTintColor:lightBG forTag:kTagSecond]; 
     [segmentedControl setTintColor:darkBG forTag:kTagThird]; 
     [segmentedControl setTextColor:darkText forTag:kTagFirst]; 
     [segmentedControl setTextColor:darkText forTag:kTagSecond]; 
     [segmentedControl setTextColor:lightText forTag:kTagThird]; 
     [segmentedControl setShadowColor:lightBG forTag:kTagFirst]; 
     [segmentedControl setShadowColor:lightBG forTag:kTagSecond]; 
     //[segmentedControl setShadowColor:lightBG forTag:kTagThird]; 
     break; 
    } 
} 

- (void)viewDidLoad 

{ 
    [segmentedControl setTag:kTagFirst forSegmentAtIndex:2]; 
    [segmentedControl setTag:kTagSecond forSegmentAtIndex:1]; 
    [segmentedControl setTag:kTagThird forSegmentAtIndex:0]; 
    [self segmentedControlChanged:nil]; 

    mapView.hidden = YES; 
    infoView.hidden = YES; 
    segmentedControl.selectedSegmentIndex = 0; 

回答

0

你需要一些邏輯移到認爲,設置默認色調顏色做負載方法您的分段欄加載。現在,你在IBAction中擁有所有的邏輯 - 在第一次選擇之前可能不會被調用。

希望有幫助

+0

這就是我認爲在ViewDidLoad下的代碼的底部。這不足以讓它知道嗎?謝謝你的幫助! –