这是简化的代码:
var body: some View {
Section(header: Text("Personal Data").position(x:45, y: 17)) {
Form {
VStack {
TextField("Title", text: self.$title)
.disabled(true)
.overlay(
Button("", action: {
self.showTitles = true
}))
.popover(isPresented: self.$showTitles, attachmentAnchor: .point(.bottom)) { EmptyView() }
Divider()
TextField("Name", text: $firstName)
Divider()
TextField("Last Name", text: $lastName)
}
.padding()
.padding(.bottom, -7)
.overlay(
RoundedRectangle(cornerRadius: 6.0)
.stroke(Color.secondary, lineWidth: 1.0)
.opacity(0.3)
)
}
}
}
在添加VStack之前,它可以按照我想要的方式工作,尽管我需要它能够放置Dividers。我试图用VStack包装单个TextField,还使用Group来查看是否只能在第一个TextField上放置按钮,但是似乎没有任何效果。为此,我必须使用GeometryReader吗?
如果有人能提供一些见解,我将不胜感激。
答案 0 :(得分:0)
这有点棘手:
SwiftUI自动检测default
button
的动作并将其用作单元格动作。因此,弹出窗口也将成为整个列表的弹出窗口(以某种方式?)
因此,您需要将buttonStyle
更改为default
以外的其他内容
TextField("Title", text: self.$title)
.disabled(true)
.overlay(
Button(action: ( { showTitles.toggle() } ),
label: ( { Text("").frame(maxWidth: .infinity, maxHeight: .infinity) } )
).buttonStyle(BorderlessButtonStyle())
)
.popover(isPresented: self.$showTitles, attachmentAnchor: .point(.bottom)) { EmptyView() }
请注意,我如何缩放按钮以填充整个空间