Showing Annotation Tools in the Main Toolbar

If you want to show annotations buttons in your app’s UINavigationBar or UIToolbar instead of in the floating annotation toolbar, then create your own UIBarButtonItem, and in its action toggle the state of the annotation state manager. This can be used to give users quick access to certain key tools such as for drawing tool or creating notes.

First create the bar button item and add it to the navigation bar. We can load the tool icon from PSPDFKit’s resources:

Copy
1
2
3
var items = pdfViewController.navigationItem.rightBarButtonItems(for: .document) ?? []
items.append(UIBarButtonItem(image: PSPDFKit.SDK.imageNamed("ink"), style: .plain, target: self, action: #selector(toggleInk)))
pdfViewController.navigationItem.setRightBarButtonItems(items, for: .document, animated: false)

And then toggle the state. This will move out of the .ink state if already in that mode or switch to the .ink mode if in any other state. This does the same thing as tapping the original button in the annotation toolbar:

1
2
@objc func toggleInk() {
    pdfViewController?.annotationStateManager.toggleState(.ink)

If you’ve added the button correctly to the navigation bar it will look like this:

One thing not covered here is updating the image of the button when the tool is selected. This can be done using something like UIGraphicsImageRenderer to draw an outlined icon.