I'm developing a reader app in SwiftUI using PDFKit. Documents are viewed with a Full Screen Cover in which the nav and status bar are hideable by tapping. Tapping once hides it, tapping again displays the nav and status bar. Problem is, the document keeps moving vertically in sync with the nav bar's height. This makes tapping on links in the document impossible and of course looks bad.
Anyone know how to fix this? Code as follows:
import PDFKit
import SwiftUI
struct PDFReaderView: View {
@Environment(\.presentationMode) var presentationMode
@State private var hideNavigationBar = false
@State private var hideStatusBar = false
let documentURL = Bundle.main.url(forResource: "PDFTest", withExtension: "pdf")!
var body: some View {
NavigationView{
PDFKitView(url: documentURL)
.toolbar{
ToolbarItemGroup(placement: .navigationBarLeading){
Button(action: {
presentationMode.wrappedValue.dismiss()
}, label: {
Image(systemName: "xmark")
})
}
}
.navigationBarTitle(Text("PDF Test"))
.navigationBarTitleDisplayMode(.inline)
//Hide navigation & status bar upon tap
.onTapGesture(count: 1){
self.hideNavigationBar.toggle()
self.hideStatusBar.toggle()
}
.navigationBarHidden(hideNavigationBar)
.statusBar(hidden: hideStatusBar)
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
The answer is to just add .ignoresSafeArea() after the toolbar{}.
NavigationView {
PDFKitRepresentedView(documentURL)
.toolbar {
//
}
.ignoresSafeArea()
//
}