如何在 HSplitView 分隔符中保留视图大小?
我有一个带有两个视图的 HSplitView
。当我想将文件拖放到此视图时,我用 在此处放置文件
标签替换该视图。如果我选择中止放置操作,视图大小会变得混乱,一个视图完全优先于另一个视图。如果我完成了掉落,一切都还好。那么,一般来说,如何在 HSplitView 中管理它呢?
下面的可重现代码。在删除文件之前,您需要将分隔符移动到另一个所需位置。
struct ContentView: View {
@State private var isDragging: Bool = false
var body: some View {
GeometryReader { geo in
if isDragging {
VStack {
Spacer()
Text("Drop file here")
Spacer()
}
} else {
HSplitView {
ZStack {
Rectangle()
.fill(.background)
Text("Some Text")
}
ZStack {
Rectangle()
.fill(.background)
Text("Some Other Text")
}
}
}
}
.padding()
.frame(width: 500, height: 500)
.onDrop(of: ["public.file-url"],
delegate:
DropFileDelegate<Any>(isDragging: $isDragging.animation()))
}
}
struct DropFileDelegate<Model>: DropDelegate {
@Binding var isDragging: Bool
func dropExited(info: DropInfo) {
isDragging = false
}
func dropUpdated(info: DropInfo) -> DropProposal? {
isDragging = true
return nil
}
func validateDrop(info: DropInfo) -> Bool {
return info.hasItemsConforming(to: ["public.file-url"])
}
func performDrop(info: DropInfo) -> Bool {
return true
}
}
I have a HSplitView
with two views. When I want to drop a file to this view, I replace the view with a Drop file here
label. If I choose to abort the drop operation, view sizes get messed up, one view takes complete priority over another. If I complete the drop, things remain okay. So, in general, how to manage this in HSplitView
?
Reproducible code below. Before dropping a file, you need to move the separator to another desired position.
struct ContentView: View {
@State private var isDragging: Bool = false
var body: some View {
GeometryReader { geo in
if isDragging {
VStack {
Spacer()
Text("Drop file here")
Spacer()
}
} else {
HSplitView {
ZStack {
Rectangle()
.fill(.background)
Text("Some Text")
}
ZStack {
Rectangle()
.fill(.background)
Text("Some Other Text")
}
}
}
}
.padding()
.frame(width: 500, height: 500)
.onDrop(of: ["public.file-url"],
delegate:
DropFileDelegate<Any>(isDragging: $isDragging.animation()))
}
}
struct DropFileDelegate<Model>: DropDelegate {
@Binding var isDragging: Bool
func dropExited(info: DropInfo) {
isDragging = false
}
func dropUpdated(info: DropInfo) -> DropProposal? {
isDragging = true
return nil
}
func validateDrop(info: DropInfo) -> Bool {
return info.hasItemsConforming(to: ["public.file-url"])
}
func performDrop(info: DropInfo) -> Bool {
return true
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论