创建主开关以更改所有背景颜色

用户10764035

我希望创建一个主开关,当它关闭时,它将每个视图控制器类的背景颜色更改为红色。不知道能不能创建一个函数来调用持有开关的类。开关将在不同的类中,但如果需要,背景也可以更改为红色。


    import UIKit
    class Switch: UIViewController {
    var toggle = UISwitch()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(toggle)
        //set frame and other properties...

        toggle.addTarget(self, action: #selector(toggleWasToggled(_:)), for: .allEvents)
          toggle.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate ([

            toggle.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :175),
            toggle.topAnchor.constraint(equalTo: view.centerYAnchor, constant : 100),
            toggle.widthAnchor.constraint(equalToConstant: 350),
            toggle.heightAnchor.constraint(equalToConstant: 180),
            ])
    }

    @objc func toggleWasToggled(_ sender: UISwitch) {
        //Whenever the switch is toggled you can post a notification. You can even post two seperate notifications, one for when the toggle is on, and one for when it is off.
        NotificationCenter.default.post(Notification(name: Notification.Name(rawValue: "ColorChange"), object: nil, userInfo: nil))

    }
    @objc func colorWasChanged(_ sender: Any) {
        view.backgroundColor = UIColor.blue
        print("espn")
    }
    }

    class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //Add an observer in all your viewControllers that need to be notified of the color change.
        NotificationCenter.default.addObserver(self, selector: #selector(colorWasChanged(_:)), name: Notification.Name(rawValue: "ColorChange"), object: nil)
    }


    @objc func colorWasChanged(_ sender: Any) {
    view.backgroundColor = UIColor.blue
        print("cnn")
    }
    }

拉杰什·库马尔

创建一个 BaseViewController 类并在此视图控制器中添加观察者并更改颜色。

class BaseViewController: UIViewController {

    static var switchStatus = false

    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(colorChanged(_:)), name: Notification.Name(rawValue: "ColorChanged"), object: nil)
    }
    @objc func colorChanged(_ notification: Notification) {
        if let switchStatus = notification.userInfo?["switchStatus"] as? Bool {
            BaseViewController.switchStatus = switchStatus
            if switchStatus {
                self.view.backgroundColor = .red
            } else {
                self.view.backgroundColor = .white
            }
        }
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if BaseViewController.switchStatus {
            self.view.backgroundColor = .red
        } else {
            self.view.backgroundColor = .white
        }
    }
}

对于从该视图控制器继承的所有其他视图控制器。无需在每个类中添加观察者。因为它是从 BaseViewController 继承的,所以它会自动添加一个观察者来改变颜色。

class ViewController: BaseViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

现在,当您更改 UISwitch 中的值时,会发布带有 UISwitch 状态的通知。Switch 类也继承自 BaseViewController。

class Switch: BaseViewController {
    var toggle = UISwitch()
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(toggle)
        //set frame and other properties...
        toggle.addTarget(self, action: #selector(toggleWasToggled(_:)), for: .allEvents)
    }
    @objc func toggleWasToggled(_ sender: UISwitch) {
        let userInfo = ["switchStatus":sender.isOn]
        NotificationCenter.default.post(Notification(name: Notification.Name(rawValue: "ColorChanged"), object: nil, userInfo: userInfo))
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

主开关控制所有儿童开关

Swift 3 - 通过开关(暗模式/夜间模式)更改所有视图控制器的背景颜色

循环更改背景颜色与javascript中的开关

无法获得开关来更改LED颜色,无论是否按下开关都只能循环显示所有颜色

如何使用javascript更改所有文本和背景的颜色?

基于主开关的灰色设置?

Android cardview,使用开关侦听器更改背景颜色

更改角度材料开关的背景颜色而不会失去波纹效果

开启开关时更改UITableViewCells的背景颜色-Swift

使用复选框类型更改开关标签的背景颜色?

如何使用 Xamarin 中的开关更改页面背景颜色

在父 div 上悬停时,更改所有子 div 的颜色以及父背景颜色

无法更改开关颜色

更改开关的“打开”颜色

更改开关的颜色

更改所有UIViewControllers背景

颜色/可绘制更改应用于具有相同背景(颜色)的所有视图[棉花糖]

如何在所有表单上更改相同的背景颜色?

如何从一种形式更改所有其他形式的背景颜色?

FullCalendar:将背景颜色更改为所有星期天

将PyCharm中的所有Jupyter笔记本背景颜色更改为白色

如何通过JavaScript更改背景颜色来覆盖对象上的所有CSS选择器?

使用事件处理程序更改背景颜色,并重绘所有 Canvas 对象

所有内容背后的 TailwindCSS 背景颜色

以编程方式更改开关的颜色

用开关单击更改颜色

更改开关的背景色

如何更改此Bootstrap 4拨动开关的“选中”背景颜色?

开关项目处于关闭模式时如何更改其背景颜色?