Ramy Al Zuhouri
Ramy Al Zuhouri

Reputation: 21996

Is it possible to add custom tools to a PKToolPicker?

If I draw for example with the Notes app I can see that there is a custom tool that can be used for handwriting. Now in my case I don't want to implement handwriting, but I need a custom tool that I can use to draw elements like arrows. In short, I need a cutomizable pencil that would allow me to detect touches and to draw directly by reading them.

Now if I read the PKTool documentation, it says:

Don’t adopt this protocol in your own objects. Instead, create a tool object to provide users with the desired the tool behavior.

But there is only a predefined set of tools. Is it possible to implement my own tool like in the Notes app?

enter image description here

Upvotes: 10

Views: 911

Answers (1)

Praveena
Praveena

Reputation: 6971

It is possible only after iOS 18

    // Create a configuration for a custom tool item.
var config = PKToolPickerCustomItem.Configuration(identifier: "com.example.custom-tool", name: "My Tool")


// Provide a custom image for the custom tool item.
config.imageProvider = { toolItem in
    guard let toolImage = UIImage(named: config.name) else { 
        return UIImage() 
    }
    return toolImage
}


// Configure additional appearance options for the custom tool item.
config.allowsColorSelection = true
config.defaultColor = .red
config.defaultWidth = 10.0


// Create a custom tool item using the configuration.
let customItem = PKToolPickerCustomItem(configuration: config)


// Create a picker with the custom tool item and a system ruler tool.
let items = [customItem, PKToolPickerRulerItem()]
let picker = PKToolPicker(toolItems: items)

More info apple doc

Upvotes: 0

Related Questions