【发布时间】:2019-11-18 17:09:20
【问题描述】:
我的应用支持 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
所以,我面临的问题不在于我的UIViewController 中的 UI 元素,而在于它所呈现的上下文菜单。
【问题讨论】:
标签: ios swift contextmenu ios13