I can not seem to get the "titleText" IBInspectable attribute working. I have a simple framework view with a UILabel which I create an IBInspectable variable "titleText". I note the inspectable "layer" variables work as expected, but not my custom "titleText" which is supposed to update the main view using this framework.
Issues are:
Interface Builder not updating the titleLabel? (i.e. at design time) i.e. I'm expecting that this should, just like it is working for the "layer" items.
At Run Time the value of titleLabel is not picking up the value I set in IB either. Note that I get the following output when I run, i.e. my code that produces this is finding the "self.titleLabel" is actually nil??
CODE SUMMARY
(a) gcCustomView.xib Snapshot
(b) gcCustomerView.swift
import UIKit
@IBDesignable
class gcCustomView: UIView {
@IBOutlet weak var titleLabel: UILabel!
@IBInspectable var titleText: String = "Default" {
didSet {
if self.titleLabel != nil {
self.titleLabel.text = titleText
} else {
NSLog("Could not set title text as label.text was nil : was trying to set to \(titleText)")
}
}
}
@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadius
layer.masksToBounds = cornerRadius > 0
}
}
@IBInspectable var borderWidth: CGFloat = 0 {
didSet {
layer.borderWidth = borderWidth
}
}
@IBInspectable var borderColor: UIColor? {
didSet {
layer.borderColor = borderColor?.cgColor
}
}
override init(frame: CGRect) {
super.init(frame: frame)
commitInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commitInit()
}
// Private
func commitInit() {
if self.subviews.count == 0 {
print("Loading Nib")
//let bundle = Bundle(forClass: self.dynamicType)
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "gcCustomView", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
view.frame = bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
addSubview(view)
}
}
}
(c) Main.storyboard Snapshot