更新到最新的Swift版本问题

问题描述:

我刚更新到最新版本的Swift,并且遇到了断点错误问题。控制台中没有错误。更新到最新的Swift版本问题

backgroundsetting.innerColor = UIColor.rgb(fromHex: 0xB0E7D7) 

backgroundsetting.outterColor = UIColor.rgb(fromHex: 0x005E7D) 

backgroundsetting.frame = (frame: CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height * 0, width:self.view.frame.size.width, height: self.view.frame.size.height)) 
self.view.addSubview(backgroundsetting) 

然后,更新后要我设置backgroundsetting.frame as! CGRect,当我打开应用程序的那部分它崩溃的应用程序。为什么会发生?这里是后代码:

backgroundsetting.innerColor = UIColor.rgb(fromHex: 0xB0E7D7) 

backgroundsetting.outterColor = UIColor.rgb(fromHex: 0x005E7D) 

backgroundsetting.frame = (frame: CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height * 0, width:self.view.frame.size.width, height: self.view.frame.size.height)) as! CGRect 
self.view.addSubview(backgroundsetting) 

backgroundsetting.frame属性是一个类型的CGRect,为什么不建立一个CGRect这样的:

backgroundsetting.frame = CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height * 0, width:self.view.frame.size.width, height: self.view.frame.size.height) 
+0

非常感谢你 –

+1

天才任何时候0等于零。为什么不'backgroundsetting.frame = view.frame'? –

+1

当然它等于零;这是一个安全防护;以防苹果发布的任何未来设备具有不同的特性 –

如果使用约束为您的视图,然后使用高度: self.view.frame.size.height和width:self.view.frame.size.width,与iPhone屏幕相比,它不会显示适当的高度和宽度。

为了更好地使用UIScreen尺寸,例如UIScreen.main.bounds.height和UIScreen.main.bounds.width,它将始终显示iPhone的原始宽度和高度。

backgroundsetting.frame = CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height * 0, width:UIScreen.main.bounds.width, height: UIScreen.main.bounds.height) as! CGRect 
    self.view.addSubview(backgroundsetting)