SwiftUI strangely shifting location of certain Views when invoking contextMenu modifier

Viewed 225

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.

1 Answers

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")
        }
Related