当我不想要它时,MDCRaisedButton在IBDesignable UIView中改变颜色

时间:2017-10-09 09:45:23

标签: swift uiview ibdesignable material-components

当我在IBDesignable UIView中使用MDCRaised时,我遇到了设置MDCRaised背景颜色的问题。从我的日志中可以看出它在viewWillAppear和viewDidAppear之间的颜色变化。

自定义视图SubmitButton包含一个MDCRaisedButton和2个可检查的backgroundColor和titleLabel.text属性。我在故事板上对这些所做的更改似乎很好。当我在我的设备上运行应用程序时,文本已更新,颜色总是变回蓝色。

我的SubmitButton的代码是 -

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.
    }
}

我的ViewController中包含日志结果的代码是 -

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

    }
}

现在我可以通过在我的视图中设置按钮的backgroundColor来解决这个问题,但这并没有解释为什么当我更喜欢在我的故事板中设置颜色而没有颜色变为蓝色时每次我使用这个自定义UIView时手动放入代码。我究竟做错了什么?我该如何解决这个问题?

0 个答案:

没有答案