在swift中更改默认的全局色调颜色

时间:2014-08-02 12:21:09

标签: swift uitabbarcontroller ios8 xcode6

我正在开发一个小应用程序,我想将默认的全局色调颜色从蓝色更改为橙​​色。我已经在Objective-C中看到了很多方法,但我似乎无法使用swift来实现它。我怎么能实现这个目标?提前谢谢!

2 个答案:

答案 0 :(得分:28)

这不容易吗?

@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {

        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

        // ...

        self.window!.tintColor = UIColor.orangeColor()

        return true
    }
}

这是Swift等同于Objective-C:

@interface AppDelegate <UIResponder, UIApplicationDelegate>

@property (nonatomic, strong) UIWindow *window;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // ...

    self.window.tintColor = [UIColor orangeColor];

    return YES;
}

@end

如果您正在使用故事板,请不要在AppDelegate中添加任何代码,只需更改&#34; Global Tint&#34;故事板的属性:

答案 1 :(得分:0)

来自 Apple here 的一个小更新。使用 Xcode 12.4 版测试。

Color Asset 目录中现在有一个 AccentColor,可以在构建设置中进行设置。在较新的项目中默认设置,但较旧的项目可能需要手动添加。

  1. 创建一个名为“AccentColor”(或任何你喜欢的颜色)的颜色资源 enter image description here
  2. 转到目标的构建设置,并将颜色资产的名称添加为“全局重点颜色名称”条目的值。 enter image description here