0

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

2 回答 2

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)
于 2017-04-03T05:00:35.657 回答
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)
于 2017-04-03T01:03:46.337 回答