skip to content

In a UIKit note app, a floating toolbar's small buttons are hard to hit and its pop-out colour palette ignores taps; how do you fix both with point(inside:with:)?

level: seniorimportance: should knowfreq 45%

answer

  1. hit testing prunes by bounds
  2. drawing outside is not touching outside
  3. a negative inset widens the claim
  4. every ancestor must claim the point too
  5. the frontmost overlapping view wins

basics

~20 s

Hit testing searches a subview only if its superview's point(inside:with:) contains the touch, so a palette outside the toolbar's bounds is unreachable. Override the toolbar's point(inside:with:) to claim its subviews' frames, and the buttons' to test an outset bounds.

solid answer

~40 s

UIKit's `hitTest(_:with:)` ignores any branch whose view's `point(inside:with:)` returns `false`, and the default returns `true` only inside `bounds`. A palette that pops out above the toolbar is drawn because `clipsToBounds` is `false`, but its touches fall outside the toolbar's bounds, so the search never reaches it and the touch lands on the canvas behind, drawing a stroke. Override the toolbar's `point(inside:with:)` to also return `true` for points inside a visible, interactive subview's frame; the default hit test then descends and finds the palette. For the small buttons, subclass `UIButton` and override `point(inside:with:)` to test `bounds.insetBy(dx: -10, dy: -10)`. The outset only works where the toolbar's own bounds or override also cover it, and where two outsets overlap the frontmost button wins.

code

swift · 22 lines
swift
import UIKit

final class ToolbarButton: UIButton {
    var touchOutset: CGFloat = 10

    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        // Grow the hit area past the 28-point artwork without changing the layout.
        bounds.insetBy(dx: -touchOutset, dy: -touchOutset).contains(point)
    }
}

final class FloatingToolbar: UIView {
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        if super.point(inside: point, with: event) { return true }
        // Claim points that land on a subview drawn outside this view's bounds,
        // such as a color palette that pops out above the toolbar.
        return subviews.contains { subview in
            !subview.isHidden && subview.isUserInteractionEnabled && subview.alpha >= 0.01
                && subview.frame.contains(point)
        }
    }
}

go deeper

for a junior

Recall that a view receives touches only inside its bounds unless point(inside:with:) is overridden.

for a middle

Explain that hit testing prunes a whole branch when a superview's point(inside:with:) is false, which is why drawing outside a view does not make that area tappable.

for a senior

Fix both cases by claiming points in the right view, keep the hidden, interaction and alpha checks, mind overlaps between outsets and every ancestor's bounds, and verify with targeted taps.

for a principal

Prefer layouts where hit areas live inside their containers, and keep the few hit-testing overrides in shared, reviewed components instead of per-screen patches.

## Why both problems are the same problem A note-taking app has a floating toolbar over its drawing canvas. The pen, highlighter and eraser buttons are drawn at 28 points, which is too small to hit reliably, and tapping the colour button pops a palette out **above** the toolbar. The palette is visible, yet tapping a colour draws a stroke on the canvas instead. Both come from how `hitTest(_:with:)` searches: - It asks each view's `point(inside:with:)` whether the point is inside, and the default answer is "inside `bounds`". - If a view says no, the documentation says its **whole branch is ignored**, including subviews drawn outside it. - Among views that say yes, the **frontmost** one that contains the point wins. So a button only gets taps inside its own bounds, and a palette outside the toolbar's bounds is never searched, however it looks on screen. `clipsToBounds` affects drawing only. ## Enlarging a small button's hit area Override `point(inside:with:)` on a `UIButton` subclass and test a larger rectangle: 1. Take `bounds` and inset it by a negative amount, for example `bounds.insetBy(dx: -10, dy: -10)`. 2. Return whether that rectangle contains the point. 3. Leave layout untouched: the artwork stays 28 points, the claimed area grows. The simplest alternative is to make the button's frame larger and keep the image centred inside it; reach for the override when the visual layout must not change. Whichever you choose, three limits apply: - **The superview must also claim the point.** If the outset pokes past the toolbar's edge, the toolbar's `point(inside:with:)` returns `false` there and the button never gets asked. - **Overlaps resolve by order.** If two buttons' outsets overlap, the one later in `subviews`, the frontmost, takes the tap. Keep each outset under half the gap between buttons. - **Controls track the touch themselves.** A `UIControl` follows the touch through `beginTracking(_:with:)` and the related tracking methods, and exposes `isTouchInside`, to decide which control events fire, so test that a touch that starts in the outset still fires `.touchUpInside` as you expect. ## Making the pop-out palette reachable Override the **toolbar's** `point(inside:with:)`: - Return `true` when `super` does. - Otherwise return `true` if the point falls inside the frame of a subview that is not hidden, has interaction enabled and has `alpha` of at least `0.01`. Because the toolbar now claims the point, the default `hitTest(_:with:)` descends into it, the palette's own `point(inside:with:)` succeeds within its bounds, and the palette's colour swatch becomes the hit-test view. Checking the hidden, interaction and alpha rules matters: without them, a hidden palette would still eat touches above the toolbar. The same rule applies all the way up. If the palette also sticks out of the toolbar's container, that container needs the same treatment; the cleaner design is often to move the palette into a view whose bounds really contain it, such as a full-width overlay that lets other touches pass through. ## Overriding hitTest(_:with:) instead You can also override the toolbar's `hitTest(_:with:)`: convert the point into each subview with `convert(_:to:)` or `convert(_:from:)`, ask the subview's own `hitTest(_:with:)`, and return the first non-`nil` result, frontmost first, before falling back to `super`. It is more code and easier to get wrong: | Approach | Keeps UIKit's skip rules | Typical use | |---|---|---| | `point(inside:with:)` override | yes, the default walk still applies them | widening or narrowing a view's area | | `hitTest(_:with:)` override | only if you re-check them or call `super` | pass-through overlays, custom routing | A hand-written `hitTest(_:with:)` that forgets `isHidden`, `isUserInteractionEnabled` and the `0.01` alpha threshold is a common source of invisible views that still catch touches. ## Checking the fix Log `hitTest(_:with:)` results for a few points, or set a breakpoint in the palette's action, and tap near the edges: inside the artwork, in the outset, in the overlap between two buttons, and on the palette above the toolbar. Each should land on the view you intend, and a tap just outside every claimed area should still reach the canvas. Calling `hitTest(_:with:)` on the window from the debugger, with a point in window coordinates and a `nil` event, shows the same routing without tapping. Repeat the check after rotation or a size change on iPad, because frames, and with them the claimed areas, move with layout.

  • Why does setting clipsToBounds to true on the toolbar make the palette problem look different but not go away?
    With `clipsToBounds` set to `true` the palette is no longer drawn outside the toolbar, so the user stops seeing a dead palette and simply sees it cut off. Hit testing behaves the same way in both cases, because the branch is pruned by `point(inside:with:)` and bounds, not by clipping. The fix is still to claim the point or to move the palette.
  • How do you choose the outset for neighbouring toolbar buttons?
    Keep the claimed areas from overlapping, or accept that the frontmost button wins every overlap. In practice you cap each outset at about half the gap between buttons and confirm the enlarged areas stay inside the toolbar's bounds, since hit testing never reaches a button through a point its superview does not claim.

  • Setting clipsToBounds to false makes the overflowing subview tappable.
  • Enlarging a button's point(inside:with:) works even past its superview's bounds.
  • Hit testing ignores point(inside:with:) overrides and uses frames directly.
  • Two overlapping hit areas both receive the tap.
  • A custom hitTest(_:with:) override automatically skips hidden views.