1

这是我的代码

struct ContentView: View {
    
    @State var showingPopover = false
    
    var body: some View {
        VStack {
            Spacer()
            Text("Hello World")
            Spacer()
            HStack {
                Spacer()
                Button {
                    self.showingPopover.toggle()
                } label: {
                    Image(systemName: "plus.circle")
                }
                .popover(isPresented: $showingPopover) {
                    
                    List(0..<100) { Text("\($0)") }
                    
                }.padding(30)
            }
        }
    }
}

这应该会产生一个来自加号按钮的非常好的弹出框。但我得到的只是一个真正被压扁的弹出框。

截屏

知道我在这里缺少什么吗?有没有办法告诉弹出框扩大更多(不指定大小)?

4

2 回答 2

2

您可以使用 aScrollViewForEach代替 a List

struct ContentView: View {
    @State var showingPopover = false

    var body: some View {
        VStack {
            Spacer()
            Text("Hello World")
            Spacer()
            HStack {
                Spacer()
                Button(action: {
                    self.showingPopover.toggle()
                }) {
                    Image(systemName: "plus.circle")
                }
                .padding(30)
            }
        }
        // can be attached to the button as well (as in the question)
        .popover(isPresented: $showingPopover,
                 attachmentAnchor: .point(.bottomTrailing),
                 arrowEdge: .bottom) {
            ScrollView(.vertical, showsIndicators: false) {
                ForEach(0 ..< 100) {
                    Text("\($0)")
                }
            }
        }
    }
}
于 2020-08-03T18:04:38.183 回答
1

您可以frameList. ScrollView另外,如果您希望它滚动,请不要忘记将 List 嵌入到 a中。

ScrollView {
    List(0..<100) {
        Text("\($0)")
    }
    .frame(width: 100, height: 250)
}
于 2020-08-03T17:53:29.943 回答