2010-01-01 57 views
9

我的問題圍繞UINavigationController UINavigationBar與UIToolbar的區別。如果我將UISegmentedControl拖放到導航欄中,如下所示:UISegmentedControl顏色在UIToolbar中

navigationBar.barStyle = UIBarStyleBlackTranslucent; 

一切都很好。 UISegmentedControl使用稍深的黑色標識選定的選項。但是,如果我將UISegmentedControl拖放到UIToolbar上,它不會從工具欄中拾取顏色或半透明度。如果我手動設置tintColor,則UISegmentedControl不再區分已選擇和未選中。

無可否認,必須先將UISegmentedControl包裝到UIBarButtonItem中,然後再放到UIToolbar上。我想知道如果這是UISegmentedControl看起來不正確(半透明的黑色背景上的藍色)的原因的一部分。

toolbar.barStyle = UIBarStyleBlackTranslucent; 
UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithCustomView:segmentedControl]; 
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:item,nil]; 
toolbar.items = toolbarItems; 

當然,我的代碼是不完全一樣的書面自從我使用的是內置的導航和控制工具欄,但一般的邏輯是一樣的。我不確定如何使UIToolbar上的UISegmentedControl具有黑色半透明風格 - 在選定和未選定的區段之間保持明顯區別。

+0

這有助於: 'segmentedController.tintColor = [的UIColor darkGrayColor];'但它創建了一個硬編碼的依賴關係,我不想介紹它。例如,讓用戶選擇配色方案稍微複雜一些。 – 2010-01-01 22:43:19

+0

你最終弄清楚了嗎? – DenNukem 2010-04-09 21:38:47

+0

直到Apple爲UISegmentedControl實現UINavigation着色邏輯到UIToolbar之前,我已被降級爲使用tintColor屬性。 – 2010-04-10 14:18:45

回答

7

似乎像:segmentedController.tintColor = [UIColor darkGrayColor];解決您的問題。

要刪除「依賴」,子類UISegmentedControl並在構造函數中設置色調。

CustomSegmentedControl.m

- (id)initWithItems:(NSArray*)items { 
    if(self = [super initWithItems:items]) { 
     self.tintColor = [UIColor darkGrayColor]; 
    } 
    return self; 
} 
+0

我認爲你只是在移動依賴關係,而不是擺脫它。例如,如果我將工具欄顏色更改爲「綠色」 - 我必須去找到像這樣的每行代碼並更改它。我寧願不這樣設置我的應用程序。我想要分段的欄「自動」饋送它的父項。事實上,它放置在UINavigationController中時會自動執行此操作。如果您將UNavigationBar樣式更改爲半透明黑色 - 任何小孩UISegmentedController都會自動跟隨套件。 – 2010-01-03 15:52:59

+0

不幸的是,當一個UISegmentedControl放在UIToolbar中時,它不會自動反映工具欄的樣式/顏色選項,正如你所說明的那樣,我必須明確地告訴它什麼顏色。在這個例子中,我將它識別爲「依賴」,我正在尋找一種不會引入這種依賴類型的解決方案。我不想在多個地方設置顏色。 – 2010-01-03 15:55:43

+0

因爲這是一個子類,所以顏色只會在代碼中出現一次。這是面向對象設計的一個特點。你可以在你的應用程序中有100個CustomSegmentedControl實例,但顏色只顯示一次。 – bentford 2010-01-04 07:16:12

相關問題