marciokoko
marciokoko

Reputation: 4986

How to set UIWindow property in Swift

Ive been trying to make a simple change in UIWindow bounds in Swift. So far I have:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions:NSDictionary?) -> Bool {
    // Override point for customization after application launch.


    //SHIFT EVERYTHING DOWN - THEN UP IN INDIV VCs IF ios7>
    var device:UIDevice=UIDevice()
    var systemVersion:String=device.systemVersion
    println(systemVersion)
    //Float(systemVersion)

    if (systemVersion >= "7.0") {
        UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
        self.window.setClipsToBounds = true // --> Member doesnt exist in UIWindow
        self.window.frame = CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height);
        self.window.bounds = CGRectMake(0,0, self.window.frame.size.width, self.window.frame.size.height);
    //}

    let ok = true
    println("Hello World")
    return true
}

but I get:

UIWindow does not have member named at each self.window.property setter line.

Upvotes: 0

Views: 4716

Answers (2)

bdjett
bdjett

Reputation: 524

In the AppDelegate class, window is an optional variable. You need to force unwrap the variable with !.

For example,

self.window!.clipsToBounds = true

If you aren't familiar with how optionals in Swift work, check out documentation from Apple about them.

Upvotes: 2

drewag
drewag

Reputation: 94723

The problem is that self.window is an Optional. You first have to "unwrap" it. You also need to use clipsToBounds not setClipsToBounds.:

if let window = self.window {
    window.clipsToBounds = true
    window.frame = CGRect(x: 0, y: 20, width: window.frame.size.width, height: window.frame.size.height);
    window.bounds = CGRect(x: 0, y: 0, width: window.frame.size.width, height: window.frame.size.height);
}

Note: I also updated CGRectMake to the preferred Swift way of creating CGRect using the CGRect initializer instead of the global CGRectMake function.

Upvotes: 3

Related Questions