Annotation State Manager

PSPDFAnnotationStateManager is responsible for all state related to annotation creation in a PSPDFViewController.

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

Using PSPDFAnnotationStateManager, you can programmatically set or toggle all the different states used for annotation creation. Additionally, when tapping on annotation tools, PSPDFAnnotationToolbar toggles annotation states while using PSPDFAnnotationStateManager 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 PSPDFAnnotationToolbar, but instead provide your own user interface related to annotations, PSPDFAnnotationStateManager 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 PSPDFAnnotationStateManager when the toolbar is made visible. This can be enabled using the following code snippet:

Copy
1
2
3
4
5
6
self.annotationToolbarController?.annotationToolbar.toolbarDelegate = self

// `PSPDFFlexibleToolbarDelegate`
func flexibleToolbarWillShow(_ toolbar: PSPDFFlexibleToolbar) {
    self.annotationStateManager.setState(.ink, variant: .inkPen)
}
Copy
1
2
3
4
5
6
self.annotationToolbarController.annotationToolbar.toolbarDelegate = self;

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

ℹ️ Important: Do not call hideToolbarAnimated: in your PSPDFViewController’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.

Copy
1
2
3
4
5
6
7
8
9
10
class CustomPDFViewController: PSPDFViewController {

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

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

}
Copy
1
2
3
4
5
6
7
8
9
10
@implementation CustomPDFViewController

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

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

@end