Annotation State Manager on iOS

AnnotationStateManager is responsible for all state related to annotation creation in a PDFViewController.

Don’t create an instance of AnnotationStateManager yourself. Instead, use the annotationStateManager exposed in PDFViewController.

Using AnnotationStateManager, you can programmatically set or toggle all the different states used for annotation creation. Additionally, when tapping on annotation tools, AnnotationToolbar toggles annotation states while using AnnotationStateManager internally. Therefore, programmatically toggling a state is automatically represented on the annotation toolbar, if it’s visible.

Custom Annotation User Interface

If you don’t want to use AnnotationToolbar, but instead provide your own user interface related to annotations, AnnotationStateManager is the class you want to use to connect your user interface to PSPDFKit.

Customize Behavior

You can also customize the behavior when showing the annotation toolbar to make it automatically select an annotation tool and immediately start drawing. This can be done by setting a state using AnnotationStateManager when the toolbar is made visible. This can be enabled using the following code snippet:

self.annotationToolbarController?.annotationToolbar.toolbarDelegate = self

// `FlexibleToolbarDelegate`
func flexibleToolbarWillShow(_ toolbar: FlexibleToolbar) {
    self.annotationStateManager.setState(.ink, variant: .inkPen)
}
self.annotationToolbarController.annotationToolbar.toolbarDelegate = self;

// `PSPDFFlexibleToolbarDelegate`
- (void)flexibleToolbarWillShow:(PSPDFFlexibleToolbar *)toolbar {
    [self.annotationStateManager setState:PSPDFAnnotationStringInk variant:PSPDFAnnotationVariantStringInkPen];
}

❗ Important: Do not call hideToolbar(animated:completion:) in your PDFViewController’s viewWillDisappear(_:):. It will exit edit mode, and it may lead to unexpected behaviors — for example, when you’re invoking the stamp picker on an iPhone, which is presented modally, viewWillDisappear(_:) will be called, thereby causing the stamp picker to be dismissed immediately. The annotation state is reset as soon as the annotation toolbar is hidden.

class CustomPDFViewController: PDFViewController {

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        // Do not hide the annotation toolbar.
        // self.annotationToolbarController?.hideToolbar(animated: animated)
    }

}
@implementation CustomPDFViewController

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    // Do not hide the annotation toolbar.
    // [self.annotationToolbarController hideToolbarAnimated:animated completion:nil];
}

@end