skip to Main Content

Context

I am currently working with the new SwiftUI ContextMenu, which supports a Preview. However, I have difficulties forcing the Preview to take up the full width of the screen.


Code

Text("Hello World")
    .contextMenu { menuItems } preview: { Text("Preview") }

Question

Please Note: I already tried to add .frame(maxWidth: .infinity) to Text("Preview") but this didn’t solve the problem.

  • How can I force the Preview to take up the full width of the screen?

2

Answers


  1. This was deprecated.enter image description here

    You didn’t give much code, so I used random example:
    You can use Menu like this:

    struct MenuExample: View {
    
    var body: some View {
        Menu {
            Button("Duplicate", action: duplicate)
            Button {
                rename()
            } label: { Label("rename", systemImage: "square.and.pencil") }
            Button {
                delete()
            } label: { Label("delete", systemImage: "trash") }
            
            Menu {
                Button("Open in Preview", action: openInPreview)
                Button("Save as PDF", action: saveAsPDF)
            } label: { Label("PDF", systemImage: "doc.fill") }
        } label: {
            Label("Menu", systemImage: "book.fill")
                .font(.title)
        }
        .foregroundColor(.orange)
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
        .padding(30)
        .background(.orange.opacity(0.3))
      }
    
        func duplicate() {}
        func delete() {}
        func rename() {}
        func saveAsPDF() {}
        func openInPreview() {}
    }
    
    struct MenuTemp_Previews: PreviewProvider {
    static var previews: some View {
        MenuExample()
     }
    }
    
    Login or Signup to reply.
  2. You could wrap your preview view in a NavigationStack. That’s easy, but it will not only expand the preview horizontally, it will also expand vertically, which may not be what you’re looking for.

    Another way would be to set the idealWidth for the preview:

    import SwiftUI
    
    struct ContentView: View {
      @State var viewWidth = CGFloat.zero
    
      var body: some View {
        VStack {
          VStack {
            Image(systemName: "globe")
              .imageScale(.large)
              .foregroundColor(.accentColor)
            Text("Long press me")
          }
          .contextMenu {
            Button("Action") {}
          } preview: {
            Text("Preview Text")
              .padding(.vertical, 50)
              .frame(idealWidth: viewWidth)
          }
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .overlay {
          GeometryReader { geometry in
            Color.clear
              .onAppear {
                self.viewWidth = geometry.frame(in: .local).size.height
              }
          }
        }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search