Display UIStepper's `value` instead of `minimumValue at app's launch

I have a UIStepper with the following parameters:

  • value: 40
  • minimumValue: 35
  • maximumValue: 45

I want to update the UILabel (linked to it) at app' launch in order to make the it displays value. It's currently displaying the minimumValue by default and I don't know why. Any ideas ?

Accepted Reply

Here is the answer 👇 that I got for the same question on stack overflow, I marked it as "Validated" ✅ :

This is an iOS bug. The value property of the stepper set in the storyboard is not honored unless the minimum value is set to 0.

For example, if you setup the stepper in the storyboard with a min value of 0, a max value of 100, a value of 50, and a step of 1, then when the view controller loads, the stepper will show a value of 50 as expected.

But if you change the min value to 1 and leave everything else the same, then the value will show as 1 instead of 50 when the view controller loads.

The simple work around is to set the initial value in code.

override func viewDidLoad() {
    super.viewDidLoad()

    sizeStepper.value = 40 // work around iOS bug
    
    // Sets the first UI's display
    showSizeValue()  
}

Here is the link 🔗 to the answer: https://stackoverflow.com/a/75738819/16067048

Replies

Showing some code related to the issue will provide more insight.

Here is some additional code from my ViewController.swift file:

  • Declaration of UIStepper and UILabel as IBOutlets lines 21 & 22:

@IBOutlet weak var sizeLbl: UILabel!

@IBOutlet weak var sizeStepper: UIStepper!

  • Initialization of sizevalue as a String line 35:

var size: String = ""

  • Initialization of showSizeValue()function in viewDidLoad() line 46:
override func viewDidLoad() {
   showSizeValue()
}
  • showSizeValue() function lines 69-73:
func showSizeValue() {
    sizeLbl.text = String(Int(sizeStepper.value))
    size = sizeLbl.text ?? ""
    updateShoeImage()
}

Here is the answer 👇 that I got for the same question on stack overflow, I marked it as "Validated" ✅ :

This is an iOS bug. The value property of the stepper set in the storyboard is not honored unless the minimum value is set to 0.

For example, if you setup the stepper in the storyboard with a min value of 0, a max value of 100, a value of 50, and a step of 1, then when the view controller loads, the stepper will show a value of 50 as expected.

But if you change the min value to 1 and leave everything else the same, then the value will show as 1 instead of 50 when the view controller loads.

The simple work around is to set the initial value in code.

override func viewDidLoad() {
    super.viewDidLoad()

    sizeStepper.value = 40 // work around iOS bug
    
    // Sets the first UI's display
    showSizeValue()  
}

Here is the link 🔗 to the answer: https://stackoverflow.com/a/75738819/16067048