【问题标题】:Detect dark mode in XCUI test on real devices在真实设备上的 XCUI 测试中检测暗模式
【发布时间】:2021-01-12 13:01:13
【问题描述】:

我正在尝试寻找一种方法来检测手机是否在 XCUI 测试中处于暗模式。到目前为止,我得到了这个:

    private func darkMode() -> Bool {
        if #available(iOS 13, *) {
            return UIView().traitCollection.userInterfaceStyle == .dark
        } else {
            return false
        }
    }

这非常适用于模拟器,但不适用于真实设备。如何在真实设备上实现这一点?

注意:我知道有一种方法可以在 XCUI 测试中设置暗/亮模式。我不要这个,我只想检测它。

【问题讨论】:

    标签: ios swift xcuitest ios-darkmode


    【解决方案1】:

    只需在您的 viewController 中使用 traitCollectionDidChange 方法,如下所示:

    //Call this method when you change UserInterface style
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        print(previousTraitCollection," preview UI mode")
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      • 2018-05-02
      • 2018-03-11
      相关资源
      最近更新 更多