Home > Blockchain >  Swift UIButton titleLabel not showing correctly
Swift UIButton titleLabel not showing correctly

Time:11-23

I am developing an iOS application with Swift / UIKit. I have a UIButton that updates its titleLabel property after a request to an API is successful when the main view appears. The problem I'm having is that it sometimes!! only displays half of the string in the UIButton. To be more specific, the request to the API returns the value of the current UVI and the title of the button is updated to "6 UVI" (for instance) and only the 6 is shown.

Here is what I do:

//Main View
class ViewController: UIViewController {
    @IBOutlet var button: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        makeAPIRequest()
    }

    func makeAPIRequest() {
        //Making the request ....
        button.setTitle("\(requestResultUV) UVI", for: .normal)
    }
}

The thing is that it only happens sometimes, so running this very same code a few times it can show the full string in the button or not, it is really weird... I would appreciate if anyone knows what's going on. Thank you!

CodePudding user response:

Alright, I have found the issue, thanks to Abdur Rehman for pointing this out. I just needed to change the style of the button to default and this makes the problem disappear. Thanks all!

  • Related