如何在从ViewController调用时设置应用程序的UIWindow颜色

时间:2016-03-10 14:59:47

标签: ios swift uiwindow

在App委托中,可以设置应用程序的UIWindow颜色,如代码所示。

但是,尝试将应用的UIWindow颜色设置为ViewController中的其他颜色时,会忽略对self.window?.backgroundColor = UIColor.blueColor()的调用。不会发生错误,但也不会发生颜色变化。

问题:

1 - 在ViewController中调用应用程序时,如何设置应用程序的窗口颜色? 注意:我不是要更改视图背景颜色,而是更改位于视图后面的UIWindow颜色。

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        self.window?.backgroundColor = UIColor.redColor()

        return true

    }

}

3 个答案:

答案 0 :(得分:3)

尝试通过AppDelegateUIApplication课程访问该窗口:

if let window = (UIApplication.sharedApplication().delegate?.window)! as UIWindow! {
    window.backgroundColor = UIColor.redColor()
}

if let window = UIApplication.sharedApplication().windows.first as UIWindow! {
    window.backgroundColor = UIColor.redColor()
}

答案 1 :(得分:0)

对于Objective-C,您可以使用以下代码:

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

    // your code
    [UIApplication sharedApplication].delegate.window.backgroundColor = [UIColor yourColor]; 

    return YES;
}

答案 2 :(得分:0)

快捷键5:

if let window = UIApplication.shared.delegate?.window as? UIWindow {
    window.backgroundColor = .red
}