2015-10-14 129 views
-2

我想要做這樣的事情: 在設置應用程序,可以選擇一種顏色,之後,像導航欄一樣的UI元素,tabbar高亮將更改爲該顏色。全球變化的UI顏色(斯威夫特)

這是否有嘖嘖的意思?

+3

你做了什麼努力? –

+0

我可以通過在每個視圖中設置導航欄顏色來做到這一點,但我想要的是在應用程序某處調用一個func並且它會全部更改 –

+0

您可以從鏈接獲取一個創意http://stackoverflow.com/questions/31217748/uicolor代碼在變量中快速 –

回答

0

下面是你如何在Objective-C中做到這一點。

- (void)setCustomizedNavigationBarStyle { 


    // UIBarButtonItem styling 
    NSShadow *shadow = [[NSShadow alloc]init]; 
    shadow.shadowOffset = CGSizeMake(0.0, 1.0); 
    shadow.shadowColor = [UIColor clearColor]; 

    NSDictionary *enabledTextAttributeDictionary = @{NSForegroundColorAttributeName : [UIColor darkGrayColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:@"GillSans" size:17.0]}; 

    [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UINavigationBar class]]] setTitleTextAttributes:enabledTextAttributeDictionary forState:UIControlStateNormal]; 

    NSDictionary *disabledTextAttributeDictionary = @{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:@"GillSans" size:17.0]}; 

    [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UINavigationBar class]]] setTitleTextAttributes:disabledTextAttributeDictionary forState:UIControlStateDisabled]; 



    // UINavigationBarTitle styling 
    NSDictionary *titleAttributeDictionary = @{NSForegroundColorAttributeName : [UIColor blackColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:@"GillSans" size:18.0]}; 

    [[UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[UINavigationController class]]]setTitleTextAttributes:titleAttributeDictionary]; 

} 

您可以在didFinishLaunchingWithOptions中打電話給我。一旦轉化爲斯威夫特,你會加入這一行didFinishLaunchingWithOptions

setCustomizedNavigationBarStyle() 

這應該是很容易翻譯成斯威夫特。

此外,您可以創建一個自定義顏色調色板。您可能會發現在該主題有幫助的這篇文章:

How do I create a category in Xcode 6 or higher?

0

你可以保存顏色NSUserDefaults,每當你需要那種顏色設爲您的視圖元素通過鍵檢索。你需要一個擴展名爲NSUserDefaults,它返回一個UIColor。

查看this question的接受答案。

希望它有幫助!