本文介绍了有什么方法可以覆盖iOS 13上下文菜单的(深色/浅色)用户界面样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序支持iOS 13暗模式,并为用户提供了匹配系统外观的选项,或强制该应用始终使用暗模式或亮模式,而与系统设置无关.

My app supports iOS 13 dark mode, and provides the user the option of matching the system appearance or forcing the app to always use either dark mode or light mode, irrespective of the system setting.

该应用程序还允许用户按下 UILabel 时显示上下文菜单.但是,当使用 UIContextMenuInteractionDelegate 方法显示上下文菜单时,我无法找到任何方法来覆盖菜单的暗/亮外观,也无法找到在以下情况下动画的 UITargetedPreview 视图的外观上下文菜单出现并消失.

The app also allows presenting a Context Menu when the user presses a UILabel. However, when presenting Context Menus using UIContextMenuInteractionDelegate methods, I cannot find any way to override the dark/light appearance of the menus, nor the appearance of the UITargetedPreview view that animates when the Context Menus appear and disappear.

例如,如果iOS外观设置为浅色模式,并且用户在应用程序中选择了强制使用深色模式的选项,则上下文菜单"将显示为浅色.我想重写该行为,使它们显得暗淡-有什么方法可以实现这一目标?我找不到与上下文菜单关联的 overrideUserInterfaceStyle 属性.

For example, if the iOS appearance is set to light mode and the user selects the option to force dark mode in the app, the Context Menus appear light. I would like to override that behavior so they appear dark - is there any way to achieve this? There seems to be no overrideUserInterfaceStyle property associated with Context Menus that I can find.

下面使用的代码仅供参考.

The code that I use is below for reference.

// Setup code

if #available(iOS 13.0, *) {
    self.textLabel.addInteraction(UIContextMenuInteraction(delegate: self))
}

// UIContextMenuInteractionDelegate

@available(iOS 13.0, *)
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
    let text = self.text
    return UIContextMenuConfiguration(identifier: nil, previewProvider: { return TextViewController(text: text) }) { [weak self] _ in
        return self?.contextMenu(for: text)
    }
}

@available(iOS 13.0, *)
private func contextMenu(for text: String) -> UIMenu {
    let copy = UIAction(title: "Copy", image: UIImage(systemName: "doc.on.doc")) { _ in
        // Perform 'text' copy
    }

    let share = UIAction(title: "Share", image: UIImage(systemName: "square.and.arrow.up")) { _ in
        // Present system share sheet
    }

    return UIMenu(title: "", children: [copy, share])
}

我使用以下命令强制显示上下文菜单的视图控制器的外观:

I force the appearance of the view controller from which the Context Menu is presented using:

overrideUserInterfaceStyle = .dark // or .light

所以,我面临的问题不是我的 UIViewController 中的UI元素,而是其中显示的上下文菜单.

So, the problem I'm facing is not with the UI elements in my UIViewController, just the Context Menus that are presented from it.

推荐答案

针对整个应用或场景覆盖明暗模式:

将以下行添加到iOS 13及更高版本中的 AppDelegate SceneDelegate 中.

self.window?.overrideUserInterfaceStyle = .light / .dark

对于特定的 UIViewController 覆盖明暗模式:

ViewDidLoad 中添加以下内容:

Override Light or Dark mode For a specific UIViewController:

Add the following like to ViewDidLoad:

self.overrideUserInterfaceStyle = .light / .dark

对于特定的 UIView 覆盖明暗模式:

Override Light or Dark mode For a specific UIView:

view.overrideUserInterfaceStyle = .light / .dark

这篇关于有什么方法可以覆盖iOS 13上下文菜单的(深色/浅色)用户界面样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 18:45