使用自定义 UIToolbar XIB 更改 UIViewController 背景颜色 Swift

Change UIViewController background color with custom UIToolbar XIB Swift

我是 swift 的新手,我想从我已经在 XIB 创建的自定义 UIToolbar 更改我的 UIViewController 背景颜色。我也已经将自定义工具栏添加到我的键盘。 我该怎么做?

在我的工具栏中,我有一个指示颜色的按钮,用户可以将其设置为 UIViewController 的背景颜色。

我的自定义工具栏

Class 用于自定义工具栏,ColorToolbar.swift

import UIKit

class ColorToolbar: UIToolbar {
    
    @IBOutlet weak var purpleButton: UIBarButtonItem!
    @IBOutlet weak var blueButton: UIBarButtonItem!
    @IBOutlet weak var greenButton: UIBarButtonItem!
    @IBOutlet weak var yellowButton: UIBarButtonItem!
    @IBOutlet weak var redButton: UIBarButtonItem!

   var makeNotesVC = MakeNotesVC()
  
    var selectedColor : UIColor = UIColor.clear

    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setupButtonColor()
    }
    
    @IBAction func colorButtonPressed(_ sender: UIBarButtonItem) {
        guard let color = sender.tintColor else {return}
        selectedColor = color
        print(selectedColor)
       makeNotesVC.view.backgroundColor = selectedColor
    }
    @objc func changeBgColor(){
        makeNotesVC.view.backgroundColor = selectedColor
    }
    private func setupButtonColor(){
        purpleButton.tintColor = Constants.TintColorButton.purple
        blueButton.tintColor = Constants.TintColorButton.blue
        greenButton.tintColor = Constants.TintColorButton.green
        yellowButton.tintColor = Constants.TintColorButton.yellow
        redButton.tintColor = Constants.TintColorButton.red
    }
}

Class 对于我想改变背景颜色的 UIViewController,MakeNotesVC.swift

import UIKit

class MakeNotesVC: UIViewController {

    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    @IBOutlet weak var titleTextfield: UITextField!
    @IBOutlet weak var notesText: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addColorbutton()
    }
    
    @IBAction func saveButtonPressed(_ sender: UIBarButtonItem) {
        let newNote = Note(context: context)
        newNote.title = titleTextfield.text
        newNote.text = notesText.text
        self.navigationController?.popToRootViewController(animated: true)
    }
    
    func addColorbutton(){
        // TODO : Add Color Options Toolbar
    
        //Register custom toolbar
        let toolbar = UINib(nibName: "Toolbar", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! ColorToolbar
        toolbar.sizeToFit()
        //Add toolbar to keyboard
        titleTextfield.inputAccessoryView = toolbar
        notesText.inputAccessoryView = toolbar
    }
  
}

在您的自定义工具栏添加协议

protocol BackgroundColorControlDelegate: AnyObject {
    func changeBackgroundColor(sender:Any, color: UIColor)
}

import UIKit

class ColorToolbar: UIToolbar {

    @IBOutlet weak var purpleButton: UIBarButtonItem!
    @IBOutlet weak var blueButton: UIBarButtonItem!
    @IBOutlet weak var greenButton: UIBarButtonItem!
    @IBOutlet weak var yellowButton: UIBarButtonItem!
    @IBOutlet weak var redButton: UIBarButtonItem!

   var makeNotesVC = MakeNotesVC()
   var selectedColor : UIColor = UIColor.clear
   weak var delegate: BackgroundColorControlDelegate?


    override func awakeFromNib() {
        super.awakeFromNib()
        setupButtonColor()
    }

    @IBAction func colorButtonPressed(_ sender: UIBarButtonItem) {
        guard let color = sender.tintColor else {return}
        selectedColor = color
        print(selectedColor)
        delegate?.changeBackgroundColor(sender: self, color: color)
    }
    @objc func changeBgColor(){
        makeNotesVC.view.backgroundColor = selectedColor
    }
    private func setupButtonColor(){
        purpleButton.tintColor = Constants.TintColorButton.purple
        blueButton.tintColor = Constants.TintColorButton.blue
        greenButton.tintColor = Constants.TintColorButton.green
        yellowButton.tintColor = Constants.TintColorButton.yellow
        redButton.tintColor = Constants.TintColorButton.red
    }
}

从 MakeNotesVC 调用委托

import UIKit

class MakeNotesVC: UIViewController {

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
@IBOutlet weak var titleTextfield: UITextField!
@IBOutlet weak var notesText: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()
    addColorbutton()
}

@IBAction func saveButtonPressed(_ sender: UIBarButtonItem) {
    let newNote = Note(context: context)
    newNote.title = titleTextfield.text
    newNote.text = notesText.text
    self.navigationController?.popToRootViewController(animated: true)
}

func addColorbutton(){
    // TODO : Add Color Options Toolbar

    //Register custom toolbar
    let toolbar = UINib(nibName: "Toolbar", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! ColorToolbar
    toolbar.sizeToFit()
    toolbar.delegate = self
    //Add toolbar to keyboard
    titleTextfield.inputAccessoryView = toolbar
    notesText.inputAccessoryView = toolbar
}

}

extension MakeNotesVC: BackgroundColorControlDelegate  {
    func changeBackgroundColor(sender:Any, color: UIColor) {
        view.backgroundColor = color
    }
}