How to change the size and position of popover's page in swiftUI?

眉间皱痕 提交于 2021-02-04 17:36:40

问题


I want to set the position and size of Popover page.

I tried all parameters of func popover, I think it may be related with attachmentAnchor and arrowEdge.

Here is my code:

    import SwiftUI

    struct ContentView : View {
        @State var isPop = false

        var body: some View {

            VStack{
                Button("Pop", action: {self.isPop = true})
                    .popover(isPresented: $isPop,  attachmentAnchor: .point(UnitPoint(x: 20, y: 20)), arrowEdge: .top, content: {return Rectangle().frame(height: 100).foregroundColor(Color.blue)})
            }

        }

    }

The effect I want:


回答1:


Here is kind of correct configuration

struct ContentView: View {
    @State private var isPop = false
    @State private var text = ""

    var body: some View {
        VStack{
            Button("Pop") { self.isPop.toggle() }
                .popover(isPresented: $isPop,
                         attachmentAnchor: .point(.bottom),   // here !
                         arrowEdge: .bottom) {                // here !!
                    VStack { // just example
                        Text("Test").padding(.top)
                        TextField("Placeholder", text: self.$text)
                            .padding(.horizontal)
                            .padding(.bottom)
                            .frame(width: 200)
                    }
            }
        }

    }
}

The attachmentAnchor is in UnitPoint, ie in 0..1 range with predefined constants, and arrowEdge is just Edge, to which popover arrow is directed. And size of popover is defined by internal content, by default it tights to minimal, but using standard .padding/.frame modifiers it can be extended to whatever needed.



来源:https://stackoverflow.com/questions/57164146/how-to-change-the-size-and-position-of-popovers-page-in-swiftui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!