iOS development
Swift programming
UIButton customization
iOS UI design
mobile app development

change text of button and disable button in iOS

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Changing a button title and disabling the button in iOS is straightforward, but the details matter when the button state is tied to asynchronous work such as a network request. The core API is setTitle(_:for:) plus isEnabled, but a good implementation also makes the disabled state visually obvious and restores the button at the right time.

Basic UIKit Pattern

For a normal UIButton, update both the title and enabled state together.

swift
1import UIKit
2
3final class ViewController: UIViewController {
4    @IBOutlet private weak var submitButton: UIButton!
5
6    @IBAction private func didTapSubmit(_ sender: UIButton) {
7        setSubmittingState()
8    }
9
10    private func setSubmittingState() {
11        submitButton.setTitle("Submitting...", for: .normal)
12        submitButton.isEnabled = false
13        submitButton.alpha = 0.6
14    }
15
16    private func restoreIdleState() {
17        submitButton.setTitle("Submit", for: .normal)
18        submitButton.isEnabled = true
19        submitButton.alpha = 1.0
20    }
21}

isEnabled = false prevents more taps, while the alpha change tells the user the control is inactive.

Update the Button on the Main Thread

UI updates belong on the main thread. That becomes important when the button triggers async work.

swift
1import UIKit
2
3final class SaveViewController: UIViewController {
4    @IBOutlet private weak var saveButton: UIButton!
5
6    @IBAction private func saveTapped(_ sender: UIButton) {
7        saveButton.setTitle("Saving...", for: .normal)
8        saveButton.isEnabled = false
9
10        Task {
11            try? await Task.sleep(nanoseconds: 1_000_000_000)
12
13            await MainActor.run {
14                self.saveButton.setTitle("Saved", for: .normal)
15                self.saveButton.isEnabled = true
16            }
17        }
18    }
19}

If you mutate UIKit state from a background thread, the behavior is unreliable and harder to debug.

Make the Disabled State Obvious

Changing only the title is often not enough. Users need feedback that the button is temporarily unavailable.

Common signals include:

  • disable interaction with isEnabled
  • reduce opacity
  • change configuration or colors
  • show a spinner or loading indicator

The goal is to communicate both progress and temporary unavailability.

Restore State on Success and Failure

Button-state bugs often show up after error paths, not success paths. If a request fails and the button stays disabled forever, the screen is effectively broken.

That is why state helpers should usually cover both transitions:

  • idle to busy
  • busy back to idle

Centralizing those transitions in small methods reduces mistakes and keeps the UI consistent.

Programmatic Buttons and Storyboard Buttons

The same API works whether the button comes from Interface Builder or is created in code. As long as you hold a reference to the UIButton, you can change its title and enabled state in exactly the same way. That consistency makes it easy to reuse the same helper logic across storyboard-based screens and programmatic UIKit layouts.

Modern Configuration-Based Buttons

On newer UIKit codebases, some buttons use UIButton.Configuration. In that case you may update configuration?.title instead of relying only on setTitle.

The broader rule is unchanged: keep title, enabled state, and visual styling in sync so the user can understand the current state immediately.

Common Pitfalls

  • Changing the button title but forgetting to disable it, allowing duplicate taps.
  • Disabling the button without enough visual feedback.
  • Updating button UI from a background thread after async work completes.
  • Forgetting to restore the button on error or cancellation.
  • Scattering button-state changes across many code paths instead of centralizing them.

Summary

  • Use setTitle(_:for:) to change the visible button text.
  • Use isEnabled = false to prevent repeated taps during work.
  • Keep UIKit updates on the main thread.
  • Add visual feedback so disabled state is obvious.
  • Centralize busy and idle transitions to keep the button behavior correct.

Course illustration
Course illustration

All Rights Reserved.