IOS Programmer
IOS Programmer

Reputation: 43

How to make UILabel show the UISlider value?

I've tried to make a slider to show a percentage out of 100, but the label doesn't show that value correctly.

Upvotes: 1

Views: 2824

Answers (3)

Genevios
Genevios

Reputation: 1145

Made for Objective C. If anybody need.

float founded = roundf(100*self.volumeViewSlider.value)/100;
float final = founded*100;

Upvotes: 0

Pranav Wadhwa
Pranav Wadhwa

Reputation: 7746

Just set the maximumValue of the slider:

slider.maximumValue = 100

This will have the slider go between 0 and 100.

However, if you would not like to do this, try something like this:

@IBAction func valueChanged(sender: UISlider) {
    let rounded = round(100 * sender.value) / 100
    let final = rounded * 100
    sliderLabel.text = "\(final)"
}

Upvotes: 1

rmaddy
rmaddy

Reputation: 318814

A UISlider has a default min and max of 0 and 1. So the slider is sending all kinds of fractional values as you move the slider such as 0.153343 and .53453545, etc. But you convert that number to an Int. That leaves you with only 0 and 1.

Either multiply the sender.value * 100 or change the slider's max value to 100.

Upvotes: 1

Related Questions