【问题标题】:SwiftUI strangely shifting location of certain Views when invoking contextMenu modifierSwiftUI 在调用 contextMenu 修饰符时奇怪地移动某些视图的位置
【发布时间】:2021-09-13 00:00:52
【问题描述】:

当我使用 .contextMenu(menuItems:) 修饰符时,我遇到了一些 SwiftUI 视图(在某些配置下)将它们的位置转移到屏幕中心的问题,我不知道为什么。我在以下代码示例中以简化的方式重现了该问题:

struct ContentView: View {
    var body: some View {
        Path { path in
            path.move(to: CGPoint(x: 200, y: 100))
            path.addLine(to: CGPoint(x: 100, y: 300))
            path.addLine(to: CGPoint(x: 300, y: 300))
            path.addLine(to: CGPoint(x: 200, y: 100))
        }
        .contextMenu {
            Text("hello world")
        }
    }
}

GIF that demonstrates how the triangle shifts to center screen for some reason due to the context menu invocation

struct ContentView: View {
    var body: some View {
        Circle()
        .position(x: 0, y: 0)
        .frame(width: 50, height: 50, alignment: .bottom)
        .contextMenu {
            Text("hello world")
        }
    }
}

GIF that demonstrates how the circle shifts to center screen and somehow gains a sharp edge due to the context menu invocation

如果有人知道这里发生了什么,我将不胜感激。我使用的是 Xcode 12.5.1、macOS 11.4 和 iOS 14.5。

【问题讨论】:

  • 这可能是position - 尝试删除它
  • @aheze 删除 position 确实有效,但不幸的是我无法解决这个抽象的问题......你知道为什么 position 会导致问题吗?如果这是一个 SwiftUI 错误,你认为值得向 Apple 提交 technical support incident 吗?

标签: swift xcode swiftui contextmenu frame


【解决方案1】:

通过为形状添加特定框架可以解决问题,但我没有任何解释。

       Path { path in
                    path.move(to: CGPoint(x: 200, y: 100))
                    path.addLine(to: CGPoint(x: 100, y: 300))
                    path.addLine(to: CGPoint(x: 300, y: 300))
                    path.addLine(to: CGPoint(x: 200, y: 100))
            }
            .frame(width: 400, height: 400, alignment: .center)
            .contextMenu {
                Text("hello world")
        }

【讨论】:

  • 有趣...添加框架会改变三角形的位置,不幸的是这对我不起作用。你认为这可能是 SwiftUI 中的一个错误吗?是否值得向 Apple 提交 technical support incident? @aheze
猜你喜欢
  • 2021-07-13
  • 2021-12-31
  • 2021-09-09
  • 2020-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多