更新到最新的Swift版本问题

时间:2017-04-03 00:58:28

标签: ios swift cgrect xcode8.2 swift3.1

我刚刚更新到最新版本的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)

2 个答案:

答案 0 :(得分:0)

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)

答案 1 :(得分:0)

如果您对视图使用约束,然后使用height: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)