Clicky

iOS Dev Nugget 288 Use the .custom UIButton type to Fix Flashing when Title is Changed

.

Need to run a code review on your codebase? Hire me

Do you sometimes you want to change the title of a text-only UIButton while it is on the screen and the text flashes? The easiest way to fix that is to use the .custom button type instead of .system.

Here's some playground code to illustrate this:

import UIKit
import PlaygroundSupport

let view = UIView()
view.backgroundColor = .green
view.frame = CGRect(x: 0, y: 0, width: 200, height: 300)
let b = UIButton(type: .system) //Switch to .custom
b.frame = CGRect(x: 10, y: 10, width: 70, height: 24)
b.setTitle("foo", for: .normal)
b.setTitleColor(.red, for: .normal)
view.addSubview(b)

PlaygroundPage.current.liveView = view

DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
    b.setTitle("bar", for: .normal)
}

.

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues