2013-03-22 128 views
13

我已經設置UISegmentedControl的使用下面的代碼外觀,如何設置默認的UISegmentedControl外觀?

UIImage *segmentSelected = [[UIImage imageNamed:@"Segment_Unselected.png"] 
          resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 12)]; 
UIImage *segmentUnselected = [[UIImage imageNamed:@"Segment_Selected.png"] 
           resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 12)]; 

[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected 
              forState:UIControlStateNormal 
             barMetrics:UIBarMetricsDefault]; 
[[UISegmentedControl appearance] setBackgroundImage:segmentSelected 
              forState:UIControlStateSelected 
             barMetrics:UIBarMetricsDefault]; 

[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                 [UIColor colorWithRed:77.0/255.0 green:45.0/255.0 blue:8.0/255.0 alpha:1],UITextAttributeTextColor, 
                 [UIColor clearColor], UITextAttributeTextShadowColor, 
                 [NSValue valueWithUIOffset:UIOffsetMake(0, 0)], UITextAttributeTextShadowOffset, 
                 [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0], UITextAttributeFont, nil] forState:UIControlStateNormal]; 

[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                 [UIColor whiteColor],UITextAttributeTextColor, 
                 [UIColor clearColor], UITextAttributeTextShadowColor, 
                 [NSValue valueWithUIOffset:UIOffsetMake(0, 0)], UITextAttributeTextShadowOffset, 
                 [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0], UITextAttributeFont, nil] forState:UIControlStateSelected]; 

[[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:@"SegmentedControl_Divider.png"] 
          forLeftSegmentState:UIControlStateNormal 
           rightSegmentState:UIControlStateNormal 
             barMetrics:UIBarMetricsDefault]; 

,我得到了完美的輸出 enter image description here

,但現在我想設置的默認外觀UISegmententer image description here

所以我必須做的?對於圖像已分配

+0

@BillWoodger我只是執行這段代碼,我做了什麼變化實際上我發現,這是在代碼中清晰可見。它似乎是一個複製粘貼錯誤。我知道大家誰都會嘗試使用此代碼最終會找到的錯誤,但我只是想救其他的時間:-) – 2014-02-07 15:31:56

+0

@BillWoodger你說得對。我應該在這裏評論這個問題。錯誤的圖像分配給上面的segmentSelected和segmentUnselected變量。 – 2014-02-07 15:34:27

+0

@AdilMalik你可以隨時添加它作爲答案自己。答案比評論更可見。如果你編輯的問題,你可能會混淆誰不知道爲什麼的答案並不反映的問題(在這個例子不一定)未來的讀者。我建議你添加一個答案,然後我們可以刪除所有這些評論以保持整潔。 – 2014-02-07 15:40:38

回答

14

把要使用默認的控制器

UIImage *segmentSelected = [[UIImage imageNamed:nil]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 12)]; 
UIImage *segmentUnselected = [[UIImage imageNamed:nil]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12, 0, 12)]; 
    
[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected 
                                forState:UIControlStateNormal 
                                barMetrics:UIBarMetricsDefault]; 
[[UISegmentedControl appearance] setBackgroundImage:segmentSelected 
                                forState:UIControlStateSelected 
                                barMetrics:UIBarMetricsDefault]; 
        
[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
           [UIColor colorWithRed:77.0/255.0 green:45.0/255.0 blue:8.0/255.0 alpha:1],UITextAttributeTextColor, 
           [UIColor clearColor], UITextAttributeTextShadowColor, 
           [NSValue valueWithUIOffset:UIOffsetMake(0, 0)], UITextAttributeTextShadowOffset, 
           [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0], UITextAttributeFont, nil] forState:UIControlStateNormal]; 
        
[[UISegmentedControl appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
           [UIColor whiteColor],UITextAttributeTextColor, 
           [UIColor clearColor],UITextAttributeTextShadowColor,  
           [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],UITextAttributeTextShadowOffset, 
           [UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0], UITextAttributeFont, nil] forState:UIControlStateSelected]; 
         
[[UISegmentedControl appearance] setDividerImage:[UIImage imageNamed:nil] 
           forLeftSegmentState:UIControlStateNormal 
           rightSegmentState:UIControlStateNormal 
           barMetrics:UIBarMetricsDefault]; 
+0

這正是我說.... – wasim 2013-03-22 04:10:00

+0

在哪裏@tosa ?? – 2014-01-02 04:56:39

+0

@WasimMalek:走了。 – 2014-01-02 09:16:41

2

刪除代碼。

+0

但我想使用默認的板塊只有一個視圖,以及其他我想自定義細分,, 我在應用程序的RootViewController的返回此代碼,因此如何重新加載默認段的外觀? – 2013-03-22 03:48:18

+0

其他視圖將代碼保留原樣。對於默認段的視圖,您可以使用界面構建器或通過代碼添加默認段。 – 2013-03-22 03:50:54

+0

我有嘗試這個界面生成的告訴我默認段,但是當我運行程序的時候,讓我發現是我年初成立的老外形段.. :( – 2013-03-22 03:54:49

4

集imagename對上面的代碼零,並把它放在你想要的默認行爲

2

設置段的圖像上的ViewController該代碼視圖控制器爲零。然後,一旦清理你的模擬器然後運行。這將是罰款....