0

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.

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

Wow! And here I thought Swift was supposed to make coding more efficient. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.