我想设置弹出页面的位置和大小。
我尝试了func popover的所有参数,我认为它可能与attachmentAnchor和arrowEdge有关。
这是我的代码:
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)})
}
}
}
我想要的效果:
答案 0 :(得分:3)
这里是正确的配置
from string import ascii_lowercase as al
index = {c: i for i, c in enumerate(al)}
def encrypt_message(s: str) -> str:
return ''.join(al[(index[c] + 2) % 26] for c in s)
>>> encrypt_message('xyz')
'zab'
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)
}
}
}
}
}
位于UnitPoint中,即在0..1范围内具有预定义的常数,而attachmentAnchor
只是Edge,弹出箭头指向该Edge。弹出窗口的大小由内部内容定义,默认情况下会缩小到最小,但是使用标准的.padding / .frame修饰符可以将其扩展为所需的任何内容。
答案 1 :(得分:0)
我发现有效的一种解决方案是在按钮周围放置填充然后偏移。我需要在填充后正确重新定位按钮的偏移量。因此,根据您的设计,只需填充即可。 ArrowEdge 参数和 attactmentAnchor 也很重要,但默认情况下,按钮下方的弹出框对我来说效果很好。
例如:
VStack{
Button("Pop", action: {self.isPop = true})
.padding(.bottom, 6) // add in padding to position the popover
.popover(isPresented: $isPop, attachmentAnchor: .point(UnitPoint(x: 20, y: 20)), arrowEdge: .top, content: {return Rectangle().frame(height: 100).foregroundColor(Color.blue)})
}