【问题标题】:CGRect is not working in closure outside of viewDidLoadCGRect 在 vi​​ewDidLoad 之外不能正常工作
【发布时间】:2019-11-17 15:17:54
【问题描述】:

我正在尝试使用CGRect 制作一个矩形形状,然后在此工作后将其返回,它将允许我对其设置约束以将其放置在我想要的位置,我在此闭包内使用它给我一个错误。这是放在我的viewDidLoad之外的

let logo : UIImageView = {
let myLogo = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.height*(77/812)*2.078, height:  self.view.frame.height*(77/812)))

    myLogo.image = #imageLiteral(resourceName: "ftrLogo")
    return myLogo

}()

【问题讨论】:

  • 你应该把它放在viewDidLoad。为什么一开始不把它放在viewDidLoad 中?
  • @Sweeper 我已经尝试过,当我这样做时,我的约束设置,放置在视图之外确实加载无法识别常量“徽标”
  • logo标记为lazy var
  • 我的意思是你应该在viewDidLoad 之外保留一个var logo: UIImageView! 属性。在viewDidLoad 中,使用logo = { ... }() 或任何你喜欢的logo。这有什么问题?
  • @Eilon 也不起作用,使用惰性变量。

标签: ios swift uiimageview closures cgrect


【解决方案1】:

您可能应该让您的徽标成为一个惰性变量。这样做:

    lazy var logo : UIImageView = {

    let myLogo = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.height*(77/812)*2.078, height:  self.view.frame.height*(77/812)))

        myLogo.image = #imageLiteral(resourceName: "ftrLogo")
        return myLogo

    }()

【讨论】:

    猜你喜欢
    • 2016-11-10
    • 2012-09-20
    • 2012-04-19
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多