更新到最新 Swift 版本问题
Updating to latest Swift version problems
我刚刚更新到 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)
如果您对视图使用约束,然后使用 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)
我刚刚更新到 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)
如果您对视图使用约束,然后使用 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)