圆形 LinearGradient 上的 ContextMenu 在 SwiftUI 中产生锐边 [英] ContextMenu on a rounded LinearGradient produces sharp edges in SwiftUI

查看:100
本文介绍了圆形 LinearGradient 上的 ContextMenu 在 SwiftUI 中产生锐边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下观点:

struct ContentView: View {
    var body: some View {
        LinearGradient(gradient: Gradient(colors: [.blue, .red]), startPoint: .topTrailing, endPoint: .bottomLeading)
            .cornerRadius(16)
            .frame(width: 140, height: 140)
            .contextMenu {
                Button("", action: {})
            }
    }

}

然而,当 ContextMenu 被调用时,边缘没有被倒圆:

However, when the ContextMenu is invoked, the edges are not rounded:

我尝试了几种方法,例如:

I've tried several things, such as:

  • 应用clipShape 修饰符将其裁剪为RoundedRectangle
  • 将渐变包装为 RoundedRectangle 视图的背景
  • 使用颜色而不是 LinearGradient(按预期工作,但不是我需要的)

然而没有工作.任何建议将不胜感激,谢谢!

However none work. Any advice would be appreciated, thanks!

推荐答案

.frame(...)后添加如下代码:

.contentShape(RoundedRectangle(cornerRadius: 16, style: .continuous))

这篇关于圆形 LinearGradient 上的 ContextMenu 在 SwiftUI 中产生锐边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆