我的应用程序支持 iOS 13 深色模式,并为用户提供匹配系统外观或强制应用程序始终使用深色模式或浅色模式的选项,无论系统设置如何。
该应用程序还允许在用户按下时显示上下文菜单UILabel
。但是,当使用以下方式呈现上下文菜单时UIContextMenuInteractionDelegate
方法,我找不到任何方法来覆盖菜单的暗/亮外观,也找不到覆盖菜单的外观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
所以,我面临的问题不在于我的 UI 元素UIViewController
,只是从中呈现的上下文菜单。