It seems that the frame property of a PDFThumbnailView is not set correctly when embedding the view (wrapped in a UIViewRepresentable) in a SwiftUI toolbar. Therefore the view is displayed with an incorrect frame and thus does not accept gestures correctly or, in some cases, the view is not displayed at all. The latter might actually not be related to the frame size, but I'm not quite sure.
Is there a way to force the frame size to be set automatically based on the toolbar size (i.e. something similar to the expanding
size policy in the Qt framework) or is there a way to get the actual size of the toolbar content area? In UIKit views had a bounds property, but in SwiftUI the view size has been hidden away for some reason.
Here are a few examples. PDFViewWrapper and PDFThumbnailViewWrapper are simple classes that wrap the corresponding PDFKit views in a UIViewRepresentable and hide some basic initialisation that is not relevant to this question.
struct DocumentView: View {
var body: some View {
PDFViewWrapper()
.toolbar {
ToolbarItem(placement: .bottomBar) {
PDFThumbnailViewWrapper()
.border(Color.red, width: 1)
.frame(minWidth: 5, minHeight: 5)
}
}
}
}
The width of the frame is set, as expected, to the full width of the toolbar, but the height remains at the manually set minimum. If the minimum wasn't forced, the height would be 0. Gestures are accepted only inside the frame even though the visual representation has been drawn outside the frame.
struct DocumentView: View {
var body: some View {
PDFViewWrapper()
.toolbar {
ToolbarItem(placement: .bottomBar) {
HStack {
PDFThumbnailViewWrapper()
.border(Color.red, width: 1)
.frame(minWidth: 5, minHeight: 5)
Button("", systemImage: "pencil.tip.crop.circle", action: {})
}
}
}
}
}
When adding another item to the toolbar, the thumbnail view disappears completely. Doesn't matter if the other item is before or after the thumbnail view.
struct DocumentView: View {
var body: some View {
PDFViewWrapper()
.toolbar {
ToolbarItem(placement: .bottomBar) {
HStack {
Button("", systemImage: "pencil.tip.crop.circle", action: {})
PDFThumbnailViewWrapper()
.border(Color.red, width: 1)
.frame(minWidth: 5, minHeight: 5)
Button("", systemImage: "pencil.tip.crop.circle", action: {})
}
}
}
}
}
When items are added both before and after the thumbnail view, then the thumbnails reappear and, for some reason, the height of the thumbnail view frame is also set correctly.
Try setting a large idealWidth
and idealHeight
on the thumbnail. If the size you set is larger than the toolbar wants to allow, the thumbnail will be given the maximum size that is allowed. In other words, the ideal size works like a max size in this case.
PDFThumbnailViewWrapper()
.border(Color.red, width: 1)
.frame(minWidth: 5, idealWidth: 1000, minHeight: 5, idealHeight: 100) // 👈 here