Xcode 9 borderColor doesn't work in User Defined Runtime Attributes

橙三吉。 提交于 2019-12-23 01:38:30

问题


I'm using this CALayer extension:

var borderUIColor: UIColor {
    set {
        self.borderColor = newValue.cgColor
    }

    get {
        return UIColor(cgColor: self.borderColor!)
    }
}

I thought that maybe because of this extension my borderColor of the Submit button from the bottom of the page doesn't change to white (as I want it to be):

But no, I hooked up an IBOutlet and tried to set directly the color like this:

submitButton.layer.borderColor = UIColor.white.cgColor

Did it in viewDidLoad, viewWillAppear and viewDidAppear because I know that in the last big update (iOS 10) the frame rendering was changed fundamentally (the 1000x1000 frame thing) and maybe there were some similar alterations now. No luck, though.

I tested in iOS 9, 10 and 11. It's not about the OS, rather about the environment. I'm using Xcode 9 Beta 5. Any ideas how to solve it?


回答1:


try decorating your var declaration with @objc like so:

@objc var borderUIColor: UIColor {
...
}

that should fix your problem




回答2:


Cleared the project, deleted the derived data and the code works now, in either viewDidLoad, viewWillAppear or viewDidAppear.

The issue is apparently with the User Defined Runtime Attributes. Xcode 9 no longer accepts extension vars here. Or, at least, the Beta 5 version.

EDIT: Indeed, adding @objc solves the issue.




回答3:


Just faced the same issue, and got another solution: I just made my class KVC-compliant.

Sample, step-by-step:

  • Custom class is XTCMenuItem, with property propIdentifier.
  • For KVC-compliance, this class got the two methods:
    • override func value(forKey key: String) -> Any?
    • override func setValue(_ value: Any?, forKey key: String)
  • In Identity Inspector, I set custom class to "XTCMenuItem", and add the user defined runtime attribute "propIdentifier" with type "String", and a string value.



回答4:


If you don't want to create IBOutlet and just use RunTime Attributes, then you can use IBDesignable and set prefix @objc it will solve your problem.

@objc @IBInspectable var borderColor: UIColor {

   get {
      return UIColor(cgColor: self.layer.borderColor!)
   }
   set {
      self.layer.borderColor = newValue.cgColor
   }
}



回答5:


Use @IBInspectable attribute. Example:

@IBInspectable var borderColor = UIColor.green


来源:https://stackoverflow.com/questions/45630049/xcode-9-bordercolor-doesnt-work-in-user-defined-runtime-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!