@IBDesignable crashing agent

前端 未结 10 919
自闭症患者
自闭症患者 2020-11-29 02:59

When I write my own UIButton-extended class and make it @IBDesignable, I receive two errors in Interface Builder, namely:

  • Main.storyb
10条回答
  •  我在风中等你
    2020-11-29 03:49

    I found something really important when using an UIImage in any class marked @IBDesignable. The classic UIImage init would crash the agent:

    let myImage = UIImage(named: String) // crash the agent
    

    The solution is to use this init method of UIImage:

    let myImage = UIImage(named: String, in: Bundle, compatibleWith: UITraitCollection)
    

    Working code example:

    let appBundle = Bundle(for: type(of: self))
    let myImage = UIImage(named: "myImage", in: bundle, compatibleWith: self.traitCollection))
    

    Where self is your class with the @IBDesignable keyword. Xcode 9.4, Swift 4.1

提交回复
热议问题