0

当我在 IBDesignable UIView 中使用它时,我在设置 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 时的代码。我究竟做错了什么?我怎样才能解决这个问题?

4

0 回答 0