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 those for drawing or creating notes.

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

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)

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:

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

If you’ve correctly added the button to the navigation bar, it’ll look like what’s shown in the following image.

If, additionally, you want to update the image of the button when the tool is selected, this can be done using something like UIGraphicsImageRenderer to draw an outlined icon.