【问题标题】:Is there any way to override the (dark/light) user interface style of iOS 13 Context Menus?有没有办法覆盖 iOS 13 上下文菜单的(暗/亮)用户界面样式?
【发布时间】: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


    【解决方案1】:

    为整个应用或场景覆盖浅色或深色模式:

    在 iOS 13 及更高版本中将以下行添加到 AppDelegateSceneDelegate

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

    为特定的UIViewController 覆盖浅色或深色模式:

    ViewDidLoad添加如下赞:

    self.overrideUserInterfaceStyle = .light / .dark
    

    为特定的UIView 覆盖浅色或深色模式:

    view.overrideUserInterfaceStyle = .light / .dark
    

    【讨论】:

    • 在带有场景的iOS 13中,您不能使用AppDelegate,您将在场景委托中设置窗口的样式。
    • @rmaddy 谢谢你的澄清。我已经更新了答案。
    • 这个答案相当令人沮丧,因为它并没有真正回答这个问题。它提供了一些与问题相关的有用信息,但未能告知如何应用这些信息来解决问题。
    • UIMenu 没有overrideUserInterfaceStyle 属性并将其设置在呈现UIViewController 上对UIMenu 的样式没有影响。所以这个答案是完全错误的。
    【解决方案2】:

    在 2022 年,我无法通过委托方法 func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? 覆盖在集合视图单元格上长按填充的 UIMenu 样式

    我的应用程序的上下文:

    1. 在 iOS 级别外观为自动
    2. 通过.overrideUserInterfaceStyle = .light 将所需的 UINavigationControllers / ViewControllers 强制转换为 .light(有时也设置在 navigationBar 上)

    试图设置与集合的子视图或其他视图相关的所有内容,只是所有具有.overrideUserInterfaceStyle 属性的内容,什么都没有。如果用户的 iOS 处于深色模式,则长按收藏视图会显示深色 UIMenu。如果用户有灯光模式,则显示灯光UIMenu

    【讨论】:

      猜你喜欢
      • 2023-01-17
      • 2020-02-22
      • 2020-07-04
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      相关资源
      最近更新 更多