繁体   English   中英

SwiftUI:如何强制 SwiftUI ContextMenu Preview 占据整个宽度?

[英]SwiftUI: How to force SwiftUI ContextMenu Preview to take up the full Width?

语境

我目前正在使用新的SwiftUI ContextMenu ,它支持Preview 但是,我很难强制Preview占据屏幕的整个宽度。


代码

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

问题

请注意:我已经尝试将.frame(maxWidth: .infinity)添加到Text("Preview")但这并没有解决问题。

  • 如何强制预览占据屏幕的整个宽度?

这已被弃用。 在此处输入图像描述

您没有提供太多代码,所以我使用了随机示例:您可以像这样使用菜单:

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()
 }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM