3

I'm having an issue where some SwiftUI views (under certain configurations) are shifting their location to the center of the screen when I use the .contextMenu(menuItems:) modifier, and I can't figure out why. I've recreated the problem in a simplified manner in the following code samples:

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

I would really appreciate it if anyone knows what's going on here. I'm on Xcode 12.5.1, macOS 11.4, and iOS 14.5.

2
  • It's probably the position - try removing that
    – aheze
    Jun 30, 2021 at 21:08
  • @aheze removing position does work but unfortunately I can't do that to solve the problem that this is abstracting... any chance you know why position is causing problems? Do you think it's worth submitting a technical support incident to Apple in case this is a SwiftUI bug?
    – jA-
    Jul 1, 2021 at 19:12

1 Answer 1

0

By adding certain frame to shape you can fix the issue but I don't have any explanation.

       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")
        }
1
  • Interesting... adding the frame changes the location of the triangle, which unfortunately won't work for me. Do you think this could be a bug in SwiftUI? Is it worth submitting a technical support incident to Apple? @aheze
    – jA-
    Jul 1, 2021 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.