我一直试图在Swift中对UIWindow边界进行简单的更改。到目前为止,我有:
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
}
但我明白了:
UIWindow没有在每个self.window.property setter行中命名的成员。
答案 0 :(得分:3)
问题是self.window
是可选的。你首先必须"unwrap" it。您还需要使用clipsToBounds
而不是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);
}
注意:我还使用CGRect初始化程序而不是全局CGRectMake
函数将CGRectMake更新为创建CGRect的首选Swift方式。
答案 1 :(得分:2)
在AppDelegate类中,window是一个可选变量。你需要用!。
强制解包变量例如,
self.window!.clipsToBounds = true
如果您不熟悉Swift中的选项如何工作,请查看Apple关于它们的文档。