UIButton
hit area
iOS development
user interface
touch targets

UIButton Making the hit area larger than the default hit area

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Small visual buttons can be hard to tap, especially on mobile screens and for users with accessibility needs. In iOS, you can enlarge a button's hit area without changing its visible size. The most robust approach is subclassing UIButton and overriding touch hit testing logic.

Why Hit Area Matters

Apple human interface guidance encourages touch targets around 44 by 44 points. If design constraints force smaller visuals, expanding hit area improves usability while preserving appearance.

This is especially important for icon only controls in toolbars and dense forms.

Subclass UIButton With Extra Insets

Create a custom button class that expands tappable bounds.

swift
1import UIKit
2
3final class HitAreaButton: UIButton {
4    var hitTestInsets = UIEdgeInsets(top: -10, left: -10, bottom: -10, right: -10)
5
6    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
7        let largerBounds = bounds.inset(by: hitTestInsets)
8        return largerBounds.contains(point)
9    }
10}

Negative inset values increase the touchable region.

Use The Custom Button In View Code

swift
1import UIKit
2
3class ViewController: UIViewController {
4    override func viewDidLoad() {
5        super.viewDidLoad()
6
7        let button = HitAreaButton(type: .system)
8        button.setImage(UIImage(systemName: "xmark"), for: .normal)
9        button.frame = CGRect(x: 100, y: 200, width: 24, height: 24)
10        button.hitTestInsets = UIEdgeInsets(top: -16, left: -16, bottom: -16, right: -16)
11        button.addTarget(self, action: #selector(closeTapped), for: .touchUpInside)
12
13        view.addSubview(button)
14    }
15
16    @objc private func closeTapped() {
17        print("Tapped")
18    }
19}

The icon remains 24 by 24 points, but touch target is much larger.

Alternative: Wrap In A Larger Container

If subclassing is not preferred, place the visible button inside a larger transparent container view and attach action there. This works, but it can complicate hierarchy and constraints compared with the subclass approach.

Subclassing keeps behavior encapsulated and reusable.

Auto Layout And Overlap Considerations

Expanded hit areas can overlap neighboring controls. In dense layouts, ensure enlarged bounds do not steal touches from adjacent buttons. Use spacing and debug overlays during UI testing.

If two expanded targets overlap, responder behavior may feel inconsistent to users.

Accessibility Pairing

Expanding hit area should be combined with accessibility labels and traits.

swift
button.accessibilityLabel = "Close"
button.accessibilityTraits = [.button]

Also verify behavior with VoiceOver and larger dynamic type settings, even for icon buttons.

Testing Strategy

Test touch accuracy on real devices, not only simulator clicks. Include one handed interactions and edge taps around the button perimeter to validate expanded region.

UI tests can tap near boundary coordinates to ensure expected target acquisition.

Reusable Design System Component

For medium and large apps, wrap hit area behavior in a shared UI component to enforce consistent touch ergonomics across screens. Teams often use multiple icon buttons, and inconsistent tap targets create uneven usability.

Define one button subclass or factory with standard hit insets and expose only limited customization points. This keeps behavior predictable and easier to test.

swift
1final class IconActionButton: HitAreaButton {
2    override init(frame: CGRect) {
3        super.init(frame: frame)
4        hitTestInsets = UIEdgeInsets(top: -12, left: -12, bottom: -12, right: -12)
5    }
6
7    required init?(coder: NSCoder) {
8        super.init(coder: coder)
9        hitTestInsets = UIEdgeInsets(top: -12, left: -12, bottom: -12, right: -12)
10    }
11}

Shared components reduce duplicated touch logic and simplify accessibility audits.

Edge Gesture Interactions

Buttons near screen edges can conflict with system gestures. Test expanded hit areas with navigation gestures to ensure taps still register as intended without breaking back swipe behavior.

Common Pitfalls

  • Enlarging hit area so much that it overlaps nearby controls.
  • Changing visual size instead of touch size when design should remain fixed.
  • Forgetting accessibility labels for icon only buttons.
  • Implementing custom touch logic in controllers instead of reusable button class.
  • Assuming simulator clicks reflect real thumb interaction behavior.

Summary

  • Expanding UIButton hit area improves usability and accessibility.
  • Override point(inside:with:) in a custom subclass for clean reuse.
  • Keep visual design unchanged while increasing touch target size.
  • Validate overlap behavior in dense layouts.
  • Pair larger hit areas with strong accessibility metadata and real device testing.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.