有什么方法可以覆盖 iOS 13 上下文菜单的 (dark/light) 用户界面样式?
Is there any way to override the (dark/light) user interface style of iOS 13 Context Menus?
我的应用程序支持 iOS 13 种深色模式,并为用户提供了匹配系统外观或强制应用程序始终使用深色模式或浅色模式的选项,无论系统设置如何。
该应用程序还允许在用户按下 UILabel
时显示上下文菜单。但是,当使用 UIContextMenuInteractionDelegate
方法呈现上下文菜单时,我找不到任何方法来覆盖菜单的 dark/light 外观,也找不到在上下文菜单出现时动画显示的 UITargetedPreview
视图的外观并消失。
例如,如果 iOS 外观设置为浅色模式,并且用户在应用中选择了强制深色模式的选项,则上下文菜单会显示为浅色。我想覆盖该行为,使它们看起来很暗 - 有什么办法可以实现这一目标吗?我找不到与上下文菜单关联的 overrideUserInterfaceStyle
属性。
我使用的代码如下,供参考。
// 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])
}
我使用以下方法强制显示上下文菜单的视图控制器的外观:
overrideUserInterfaceStyle = .dark // or .light
所以,我面临的问题不在于 UIViewController
中的 UI 元素,而在于从中呈现的上下文菜单。
覆盖整个应用程序或场景的明暗模式:
将以下行添加到 AppDelegate
或 iOS 13 及更高版本中的 SceneDelegate
。
self.window?.overrideUserInterfaceStyle = .light / .dark
覆盖浅色或深色模式对于特定 UIViewController
:
为ViewDidLoad
添加以下赞:
self.overrideUserInterfaceStyle = .light / .dark
为特定的 UIView
覆盖浅色或深色模式:
view.overrideUserInterfaceStyle = .light / .dark
2022 年,我无法通过委托方法 func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration?
覆盖 collection 视图单元格上 long-press 上填充的 UIMenu 的样式
我的应用的上下文:
- 在 iOS 级别 自动外观
- 强制需要 UINavigationControllers / ViewControllers 通过
.overrideUserInterfaceStyle = .light
.light(有时也在导航栏上设置)
试图设置与 collection 的子视图或其他视图相关的所有内容,仅设置具有 .overrideUserInterfaceStyle
属性 的所有内容,但什么也没有。如果用户 iOS 处于深色模式,collection 视图上的 Long-press 会显示深色 UIMenu
。如果用户有灯光模式,则显示灯光 UIMenu
。
我的应用程序支持 iOS 13 种深色模式,并为用户提供了匹配系统外观或强制应用程序始终使用深色模式或浅色模式的选项,无论系统设置如何。
该应用程序还允许在用户按下 UILabel
时显示上下文菜单。但是,当使用 UIContextMenuInteractionDelegate
方法呈现上下文菜单时,我找不到任何方法来覆盖菜单的 dark/light 外观,也找不到在上下文菜单出现时动画显示的 UITargetedPreview
视图的外观并消失。
例如,如果 iOS 外观设置为浅色模式,并且用户在应用中选择了强制深色模式的选项,则上下文菜单会显示为浅色。我想覆盖该行为,使它们看起来很暗 - 有什么办法可以实现这一目标吗?我找不到与上下文菜单关联的 overrideUserInterfaceStyle
属性。
我使用的代码如下,供参考。
// 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])
}
我使用以下方法强制显示上下文菜单的视图控制器的外观:
overrideUserInterfaceStyle = .dark // or .light
所以,我面临的问题不在于 UIViewController
中的 UI 元素,而在于从中呈现的上下文菜单。
覆盖整个应用程序或场景的明暗模式:
将以下行添加到 AppDelegate
或 iOS 13 及更高版本中的 SceneDelegate
。
self.window?.overrideUserInterfaceStyle = .light / .dark
覆盖浅色或深色模式对于特定 UIViewController
:
为ViewDidLoad
添加以下赞:
self.overrideUserInterfaceStyle = .light / .dark
为特定的 UIView
覆盖浅色或深色模式:
view.overrideUserInterfaceStyle = .light / .dark
2022 年,我无法通过委托方法 func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration?
我的应用的上下文:
- 在 iOS 级别 自动外观
- 强制需要 UINavigationControllers / ViewControllers 通过
.overrideUserInterfaceStyle = .light
.light(有时也在导航栏上设置)
试图设置与 collection 的子视图或其他视图相关的所有内容,仅设置具有 .overrideUserInterfaceStyle
属性 的所有内容,但什么也没有。如果用户 iOS 处于深色模式,collection 视图上的 Long-press 会显示深色 UIMenu
。如果用户有灯光模式,则显示灯光 UIMenu
。