> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/ios/viewer/viewer/new-ui-customization-ios.md).

# Customize the Document Viewer UI

Learn how to customize and manipulate toolbar items in your app using a PTDocumentController. Hide, show, remove, and add buttons and toolbars with code examples provided. Optimize your app's user int

The following snippets assume you are using a `PTDocumentController` called `documentController`:

{% tabs %}
{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
PTDocumentController *documentController = [[PTDocumentController alloc] init];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
let documentController = PTDocumentController()
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Hide the Toolbar Switcher

{% tabs %}
{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
documentController.toolGroupIndicatorView.hidden = YES;
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
documentController.toolGroupIndicatorView.isHidden = true
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Hide (and show) the annotation toolbar

The toolbar can be programmatically hidden by setting the mode to view group, which is a special group and the only group where the toolbar is hidden:

{% tabs %}
{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
documentController.toolGroupManager.selectedGroup = documentController.toolGroupManager.viewItemGroup;
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
documentController.toolGroupManager.selectedGroup = documentController.toolGroupManager.viewItemGroup
```

{% endcode %}
{% endtab %}
{% endtabs %}

The toolbar can be shown again by changing the group to any group other than view:

{% tabs %}
{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
documentController.toolGroupManager.selectedGroup = documentController.toolGroupManager.drawItemGroup;
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
documentController.toolGroupManager.selectedGroup = documentController.toolGroupManager.drawItemGroup
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Remove toolbars from the switcher

Toolbars can be removed by removing them from the toolGroupManager's groups array. The following code removes the "Draw" and "Pens" toolbars:

{% tabs %}
{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
NSMutableArray<PTToolItemGroup*>* mutableGroups = [documentController.toolGroupManager.groups mutableCopy];
    
[mutableGroups removeObjectsInArray:@[documentController.toolGroupManager.drawItemGroup, documentController.toolGroupManager.pensItemGroup]];
    
documentController.toolGroupManager.groups = [mutableGroups copy];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
var mutableGroups = documentController.toolGroupManager.groups

let groupsToRemove = [documentController.toolGroupManager.drawItemGroup, documentController.toolGroupManager.pensItemGroup]

mutableGroups.removeAll(where: {groupsToRemove.contains($0)})

documentController.toolGroupManager.groups = mutableGroups
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Remove buttons from a toolbar

The example below shows how to remove the text highlight and text underline button from a toolbar.

{% hint style="info" %}
**Disabling a tool type entirely**

If you want to disable a tool entirely, from all toolbars and the long press menu, please use the [annotations permissions](/ios/annotation/customization.md#create-and-edit-annotations) system.
{% endhint %}

{% tabs %}
{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
PTToolItemGroup* annotateGroup = documentController.toolGroupManager.annotateItemGroup;

// tool buttons that exist currently
NSArray<UIBarButtonItem*>* defaultAnnotateGroupTools = annotateGroup.barButtonItems;

// new set of tools to replace current ones
NSMutableArray<UIBarButtonItem*>* newAnnotateGroupTools = [[NSMutableArray alloc] init];

// add all currently existing tools except for the ones we don't want
for(UIBarButtonItem* defaultToolItem in defaultAnnotateGroupTools)
{
    if( [defaultToolItem isKindOfClass:[PTToolBarButtonItem class]] )
    {
        PTToolBarButtonItem* toolBarButton = (PTToolBarButtonItem*)defaultToolItem;
        
        if( toolBarButton.toolClass == [PTTextHighlightCreate class] ||
             toolBarButton.toolClass == [PTTextUnderlineCreate class] )
        {
            // do not add this tool
            continue;
        }
        else
        {
            [newAnnotateGroupTools addObject:defaultToolItem];
        }
    }
    else
    {
        [newAnnotateGroupTools addObject:defaultToolItem];
    }
}

// assign tools to new array
documentController.toolGroupManager.annotateItemGroup.barButtonItems = [newAnnotateGroupTools copy];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
let annotateGroup = documentController.toolGroupManager.annotateItemGroup

// tool buttons that exist currently
let defaultAnnotateGroupTools = annotateGroup.barButtonItems

// new set of tools to replace current ones
var newAnnotateGroupTools = [UIBarButtonItem]()

// add all currently existing tools except for the ones we don't want
for defaultToolItem in defaultAnnotateGroupTools
{
    if defaultToolItem.isKind(of: PTToolBarButtonItem.self) {
        let toolBarButton = defaultToolItem as! PTToolBarButtonItem
        if toolBarButton.toolClass == PTTextHighlightCreate.self ||
            toolBarButton.toolClass == PTTextUnderlineCreate.self
        {
            // do not add this tool
            continue
        }
        else
        {
            newAnnotateGroupTools.append(toolBarButton)
        }
    }
    else
    {
        newAnnotateGroupTools.append(defaultToolItem)
    }
}

// assign tools to new array
documentController.toolGroupManager.annotateItemGroup.barButtonItems = newAnnotateGroupTools
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Add a tool button to a toolbar

{% tabs %}
{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
// create a mutable array of the current items in the annotation toolbar group
NSMutableArray<UIBarButtonItem*>* availableTools = [documentController.toolGroupManager.annotateItemGroup.barButtonItems mutableCopy];

// create a new toolbar item for freehand annotations
UIBarButtonItem* freeHandItem = [documentController.toolGroupManager createItemForToolClass:[PTFreeHandCreate class]];

// add the freehand annotation item to the front of the list
[availableTools insertObject:freeHandItem atIndex:0];

// assign the array back to the annotation toolbar group.
documentController.toolGroupManager.annotateItemGroup.barButtonItems = [availableTools copy];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
// create a mutable array of the current items in the annotation toolbar group
var availableTools:[UIBarButtonItem] = documentController.toolGroupManager.annotateItemGroup.barButtonItems

// create a new toolbar item for freehand annotations
let freeHandItem = documentController.toolGroupManager.createItem(forToolClass: PTFreeHandCreate.self)

// add the freehand annotation item to the front of the list
availableTools.insert(freeHandItem, at: 0)

// assign the array back to the annotation toolbar group.
documentController.toolGroupManager.annotateItemGroup.barButtonItems = availableTools
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Create a new toolbar

The code below creates a new toolbar that contains a free hand, cloudy and image stamp tool.

{% tabs %}
{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
// the image that will be used in the toolbar switcher menu
UIImage* image = [UIImage systemImageNamed:@"square.and.pencil"];

// the tools it will contain
UIBarButtonItem* freeHandItem = [documentController.toolGroupManager createItemForToolClass:[PTFreeHandCreate class]];
UIBarButtonItem* cloudyItem = [documentController.toolGroupManager createItemForToolClass:[PTCloudCreate class]];
UIBarButtonItem* stampItem = [documentController.toolGroupManager createItemForToolClass:[PTImageStampCreate class]];

// the name of the custom group, its image, and its tool items
PTToolItemGroup* customGroup = [PTToolItemGroup groupWithTitle:@"MyApps Group" image:image barButtonItems:@[freeHandItem, cloudyItem, stampItem]];

// add the tool group
documentController.toolGroupManager.groups = [documentController.toolGroupManager.groups arrayByAddingObject:customGroup];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
// the image that will be used in the toolbar switcher menu
let image = UIImage(systemName:"square.and.pencil")

// the tools it will contain
let freeHandItem = documentController.toolGroupManager.createItem(forToolClass:PTFreeHandCreate.self)
let cloudyItem = documentController.toolGroupManager.createItem(forToolClass:PTCloudCreate.self)
let stampItem = documentController.toolGroupManager.createItem(forToolClass:PTImageStampCreate.self)

// the name of the custom group, its image, and its tool items
let customGroup:PTToolGroup = PTToolGroup(title: "MyApps Group", image: image, barButtonItems: [freeHandItem, cloudyItem, stampItem])

// add the tool group
documentController.toolGroupManager.groups.append(customGroup)
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Add a button with fully custom behavior

Your app may need a button that does not invoke one of the built in annotation tools. The following code will add a button that calls a selector.

{% tabs %}
{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
PTSelectableBarButtonItem* selectableItem = [[PTSelectableBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(customToolAction:)];
selectableItem.title = @"Custom Tool";

documentController.toolGroupManager.annotateItemGroup.barButtonItems = [documentController.toolGroupManager.annotateItemGroup.barButtonItems arrayByAddingObject:selectableItem];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
let selectableItem = PTSelectableBarButtonItem(image: UIImage(systemName:"square.and.pencil"), style: .plain, target: self, action: #selector(customToolAction(_:)))
selectableItem.title = "Custom Tool"

documentController.toolGroupManager.annotateItemGroup.barButtonItems.append(selectableItem)
```

{% endcode %}
{% endtab %}
{% endtabs %}

If you want to toggle the items selection, flip its selected property:

{% tabs %}
{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
-(void)customToolAction:(PTSelectableBarButtonItem*)button
{
    button.selected = !button.selected;
}
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
@objc func customToolAction(_ button:PTSelectableBarButtonItem)
{
    button.isSelected = !button.isSelected
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Modify `UINavigationBar` Items

You can modify the navigation bar items. Here is an example of adding a new button (for the current size class):

{% tabs %}
{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
UIBarButtonItem* myItem = [[UIBarButtonItem alloc] initWithImage:[UIImage systemImageNamed:@"square.and.pencil"] style:UIBarButtonItemStylePlain target:nil action:nil];

documentController.navigationItem.rightBarButtonItems = [documentController.navigationItem.rightBarButtonItems arrayByAddingObject:myItem];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
let myItem = UIBarButtonItem(image: UIImage(systemName:"square.and.pencil"), style: .plain, target: nil, action: nil)

documentController.navigationItem.rightBarButtonItems.append(myItem)
```

{% endcode %}
{% endtab %}
{% endtabs %}

Tools can also be added:

{% tabs %}
{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
UIBarButtonItem* freeHand = [documentController.toolGroupManager createItemForToolClass:[PTFreeHandCreate class]];

documentController.navigationItem.rightBarButtonItems = [documentController.navigationItem.rightBarButtonItems arrayByAddingObject:freeHand];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
let freeHand = documentController.toolGroupManager.createItem(forToolClass:PTFreeHandCreate.self)

documentController.navigationItem.rightBarButtonItems.append(freeHand)
```

{% endcode %}
{% endtab %}
{% endtabs %}

Note that in the example above, de-selecting the tool button item needs to be implemented by the app, by listening to the Tool Did Change notification.

## Add and Remove Toolbar Items (iPhone UI)

These are the buttons that appear at the bottom of the screen.

{% tabs %}
{% tab title="Obj-C" %}
{% code lineNumbers="true" %}

```objc
// new button
UIBarButtonItem* myItem = [[UIBarButtonItem alloc] initWithImage:[UIImage systemImageNamed:@"square.and.pencil"] style:UIBarButtonItemStylePlain target:nil action:nil];
   
// spacer to keep evenly spaced buttons
UIBarButtonItem* spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

// new array
NSMutableArray* toolbarItems = [documentController.toolbarItems mutableCopy];

// add the new items
[toolbarItems addObjectsFromArray:@[spacer, myItem]];

// set the toolbarItems to the new items
documentController.toolbarItems = [toolbarItems copy];
```

{% endcode %}
{% endtab %}

{% tab title="Swift" %}
{% code lineNumbers="true" %}

```swift
// new button
let myItem = UIBarButtonItem(image: UIImage(systemName:"square.and.pencil"), style: .plain, target: nil, action: nil)

// spacer to keep evenly spaced buttons
let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)

// new array
var toolbarItems = documentController.toolbarItems

// add the new items
toolbarItems.append(contentsOf: [spacer, myItem])

// set the toolbarItems to the new items
documentController.toolbarItems = toolbarItems
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/ios/viewer/viewer/new-ui-customization-ios.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
