I'm having a problem with setting the background colour of an MDCRaised when I use it in an IBDesignable UIView. It's seems from my logs its changing colour between viewWillAppear and viewDidAppear.
The custom view SubmitButton contains an MDCRaisedButton and 2 inspectable properties for its backgroundColor and titleLabel.text. Changes I make to these on the storyboard appear fine. When I run the app on my device the text has update the colour always changes back to a blue colour.
The code for my SubmitButton is -
import UIKit
import MaterialComponents
@IBDesignable
class SubmitButton: UIView {
public var view:UIView!
@IBOutlet weak var button: MDCRaisedButton!
@IBInspectable dynamic var buttonText:String = "Submit button text" {
didSet {
button.setTitle(buttonText, for: .normal)
button.setTitle(buttonText, for: .highlighted)
}
}
@IBInspectable dynamic var buttonColour:UIColor? {
didSet {
button.setBackgroundColor(buttonColour, for: .normal)
print("Did set button colour = \(button.backgroundColor!)")
// Prints - Did set button colour = UIExtendedGrayColorSpace 0 1
button.setBackgroundColor(buttonColour, for: .normal)
button.setBackgroundColor(buttonColour, for: .highlighted)
}
}
public override init(frame: CGRect) {
super.init(frame: frame)
nibSetup()
styleDisplay()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
nibSetup()
styleDisplay()
}
public func nibSetup() {
view = loadViewFromNib()
view.frame = bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.translatesAutoresizingMaskIntoConstraints = true
addSubview(view)
}
public func loadViewFromNib() -> UIView {
let aClass: AnyClass = self.classForCoder
if aClass == SubmitButton.classForCoder() {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
let nibView = nib.instantiate(withOwner: self, options: nil).first as! UIView
return nibView
}
let bundle = Bundle(for: type(of: SubmitButton()))
let nib = UINib(nibName: String(describing: type(of: SubmitButton())), bundle: bundle)
let nibView = nib.instantiate(withOwner: self, options: nil).first as! UIView
return nibView
}
func styleDisplay() {
self.backgroundColor = .clear
//Set any other UI code here.
}
}
The code from my ViewController with results from log is -
import MaterialComponents
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var raiseButton: MDCRaisedButton!
@IBOutlet weak var submitButton: SubmitButton!
override func viewDidLoad() {
super.viewDidLoad()
print("Colour at viewDidLoad \(String(describing: submitButton.button.backgroundColor!))")
//prints - Colour at viewDidLoad UIExtendedGrayColorSpace 0 1
}
override func viewWillAppear(_ animated: Bool) {
print("Colour at viewWillAppear \(String(describing: submitButton.button.backgroundColor!))")
//prints - Colour at viewWillAppear UIExtendedGrayColorSpace 0 1
}
override func viewDidAppear(_ animated: Bool) {
print("Colour at viewDidAppear \(String(describing: submitButton.button.backgroundColor!))")
//prints - Colour at viewDidAppear UIExtendedSRGBColorSpace 0.129412 0.588235 0.952941 1 - The blue color
}
}
Now I can arguably fix the problem by setting the backgroundColor of button in my view will appear method but this doesn't explain why it's changing colour to blue when I'd prefer to just set the colour in my storyboards and not have to manually put code in each time I use this custom UIView. What am I doing wrong? How can I fix this?