SwiftUI在上下文菜单打开时删除白色颜色

u4vypkhs  于 2024-01-05  发布在  Swift
关注(0)|答案(2)|浏览(130)

我尝试使用快捷菜单是SwiftUI,我面临的问题,白色背景出现在我如何才能删除白色背景?

  1. struct PlayerLayerView: View {
  2. var body: some View {
  3. VideoPlayer(player: AVPlayer(url: URL(string: "https://i.imgur.com/2f4ipZX.mp4")!))
  4. .contextMenu {
  5. Button {} label: {
  6. Text("item")
  7. }
  8. Button {
  9. } label: {
  10. Text("item")
  11. }
  12. } preview: {
  13. VideoPlayer(player: AVPlayer(url: URL(string: "https://i.imgur.com/2f4ipZX.mp4")!))
  14. }
  15. }
  16. }
  17. #Preview {
  18. PlayerLayerView()
  19. }

字符串
video example

hmae6n7t

hmae6n7t1#

如果上下文菜单附加到覆盖层上,似乎看不到白色背景。麻烦的是,如果覆盖层覆盖了整个区域,那么它也会阻止所有手势,所以它会阻止与视频播放器的交互。
我尝试使用以下由透明区域组成的覆盖层,以允许手势在某些地方通过:

  1. VStack(spacing: 0) {
  2. Color.clear.frame(height: 50)
  3. Color.white.opacity(0.001)
  4. Color.clear.frame(height: 50)
  5. Color.white.opacity(0.001)
  6. Color.clear.frame(height: 50)
  7. }

字符串
这是有效的,但它仍然不令人满意,因为你希望能够点击暂停的视频的任何地方,以获得控制显示。
然而,我发现一个类似按钮的小控件工作得很好。它也可以被标记,以明确其用途:

  1. VideoPlayer(player: AVPlayer(url: URL(string: "https://i.imgur.com/2f4ipZX.mp4")!))
  2. .overlay(alignment: .bottom) {
  3. Text("Hold for preview")
  4. .foregroundStyle(.white)
  5. .padding(.horizontal, 20)
  6. .padding(.vertical, 10)
  7. .background {
  8. Capsule()
  9. .fill(.gray.opacity(0.5))
  10. }
  11. .contentShape(.contextMenuPreview, Capsule())
  12. .padding(50)
  13. .contextMenu {
  14. Button {} label: {
  15. Text("item")
  16. }
  17. Button {
  18. } label: {
  19. Text("item")
  20. }
  21. } preview: {
  22. VideoPlayer(player: AVPlayer(url: URL(string: "https://i.imgur.com/2f4ipZX.mp4")!))
  23. }
  24. }


的数据

展开查看全部
qzlgjiam

qzlgjiam2#

在SwiftUI中,contextMenu修饰符创建了一个带有默认背景的上下文菜单。您需要使用自定义视图和手势的合并来模拟没有背景的上下文菜单。

相关问题