Contents

UIToolTipInteractionDelegate

An interface that provides tooltip settings to an interaction.

Declaration

@MainActor protocol UIToolTipInteractionDelegate : NSObjectProtocol

Overview

A tooltip interaction delegate provides configuration information about a tooltip. You can, for instance, use the delegate to change the text of a tooltip.

To change the tooltip text, set a tooltip interaction’s delegate property to an object that conforms to the UIToolTipInteractionDelegate protocol and implements the toolTipInteraction(_:configurationAt:) method. For example, in the following code listing, the code creates a new view and assigns its background color. Then the code creates a UIToolTipInteraction object, sets the delegate equal to the view, and adds the interaction to the view.

lazy var viewWithBackgroundColorTooltip: UIView = {
    let view = ViewWithBackgroundColorTooltip()
    view.backgroundColor = UIColor.systemYellow
    
    let tooltipInteraction = UIToolTipInteraction()
    tooltipInteraction.delegate = view
    view.addInteraction(tooltipInteraction)
    
    return view
}()

The delegate’s implementation of the toolTipInteraction(_:configurationAt:) method looks up the accessibilityName of the view’s background color. If the name is available, the method creates a UIToolTipConfiguration object, passing in the name of the color. Then the method returns the configuration. If the name isn’t available, the method returns nil, which prevents the display of the tooltip.

class ViewWithBackgroundColorTooltip: UIView, UIToolTipInteractionDelegate {
    
    func toolTipInteraction(_ interaction: UIToolTipInteraction, configurationAt point: CGPoint) -> UIToolTipConfiguration? {

        let configuration: UIToolTipConfiguration?
        if let accessibilityName = backgroundColor?.accessibilityName {
            configuration = UIToolTipConfiguration(toolTip: "The color is \(accessibilityName).")
        } else {
            configuration = nil
        }
        
        return configuration
    }

}

In addition to changing the tooltip text, you can also specify the region within a view or control where a person must position the pointer to trigger the display of the tooltip. To specify the region, create the tooltip configuration using the init(toolTip:in:) method.

Topics

Providing a tooltip configuration

See Also

Tooltips