我在SwiftUI中的圆角视图上有ContextMenu的错误。

huangapple go评论50阅读模式
英文:

I have bug with ContextMenu on rounded views in SwiftUI

问题

我在调用上下文菜单时,我的视图显示存在问题。没有上下文菜单时,视图显示正常,但出现问题时似乎存在上下文菜单。如果有人有关于如何解决这个问题的建议,我将非常感激。

Contex View
Travel View]

TravelView(travel: travel)
   .contextMenu {
      VStack {
         Button(action: {}) {
            Label("Edit", systemImage: "pencil")
         }
      }
   }

TravelView如下:

VStack {
   // 一些代码
}
.background(Color.green.opacity(0.8))
.cornerRadius(20)

我需要去掉胶囊上的边框和字体上的阴影。

英文:

I'm experiencing an issue with the display of my View when I invoke the context menu. The View appears correctly without the context menu, but there seems to be a problem when it's present. If anyone has any suggestions on how to fix this, I would greatly appreciate it.

Contex View
Travel View]

TravelView(travel: travel)
       .contextMenu {
                     VStack {
                     Button(action: {}) {
                     Label("Edit", systemImage: "pencil")
                                            }
                      }

TravelView looks like

VStack{

     // some code 
      }
 
    .background(Color.green.opacity(0.8))
    .cornerRadius(20)
    .shadow(color: .black.opacity(0.5), radius: 5, x: 0, y: 2)

I need to remove the frames on the capsule and the shadows on the font

答案1

得分: 2

.contentShape(.contextMenuPreview, RoundedRectangle(cornerRadius: 20)) 请放在.contextMenu 修饰符之前,就像这样:

TravelView(travel: travel)
    .contentShape(.contextMenuPreview, RoundedRectangle(cornerRadius: 20)) // 这里的20是在你的 `TravelView` 中使用的圆角半径
    .contextMenu {
        VStack {
            Button(action: {}) {
                Label("编辑", systemImage: "pencil")
            }
        }
    }

希望能解决问题。

英文:

Add
.contentShape(.contextMenuPreview, RoundedRectangle(cornerRadius: 20)) just before the .contextMenu modifier like this:

TravelView(travel: travel)
    .contentShape(.contextMenuPreview, RoundedRectangle(cornerRadius: 20)) // this 20 is the corner radius used in your `TravelView`
    .contextMenu {
        VStack {
            Button(action: {}) {
                Label("Edit", systemImage: "pencil")
            }
        }

Hope it solves the issue.

huangapple
  • 本文由 发表于 2023年6月8日 23:52:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433667.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定