Can't set the slider to 0

I'm trying to make my sliders at 0 make no shape but the issue is when any slider hits 0 it makes the shape go to its maximum size. What should I do?

Code Block
func goToZero() {
    if otSlider.value == 0 {
      otWidth.constant = 0
      print(otWidth.constant)
    } else if efSlider.value == 0 {
      efWidth.constant = 0
    } else if ifSlider.value == 0 {
      ifWidth.constant = 0
    }
  }

Answered by ZoneX in 651018022
Alright I made a func that fixes the issue using the alpha.

Code Block
func goToZero() {
    if otSlider.value == 0 {
      otShape.alpha = 0
    } else if efSlider.value == 0 {
      efShape.alpha = 0
    } else if ifSlider.value == 0 {
      ifShape.alpha = 0
    } else {
      otShape.alpha = 1
      efShape.alpha = 1
      ifShape.alpha = 1
    }
  }

Which slider do you want to set to zero ?

What is otWidth.constant ? Is it a constraint ?
What do you want to set to zero ? The width of the slider ?

To set the value of a slider, use setValue:
Code Block
func setValue(Float, animated: Bool)

In addition,
Code Block
if otSlider.value == 0

the value is already zero, what do you want to change ?

So please clarify.
OtWidth.constant is the width of the shape it's not constant which is ironic. I want when each of the sliders becomes 0 to not show anything.
You should call setNeedsLayout()

Code Block
func goToZero() {
if otSlider.value == 0 {
otWidth.constant = 0
otSlider.setNeedsLayout()
print(otWidth.constant)
} else if efSlider.value == 0 {
efWidth.constant = 0
efSlider.setNeedsLayout()
} else if ifSlider.value == 0 {
ifWidth.constant = 0
ifSlider.setNeedsLayout()
}
}

It still does not seem to work.

Does not seem to work

That's a bit vague.

What do you get ?
Please show more code, where you define and set otwidth. Show the full code of the class, including var and IBOutlets declarations.

They should not only be var, but IBOutlets connected to the constraint.


I will give you what I do for one of them since the rest are the same, for ot I have one variable and four outlets, in a class called PoVC

Code Block
var otPerNum: Int = 0 //number I use in the textField
@IBOutlet weak var otStack: UIStackView! //I use this to get the maximum value and set the value for the slider in the beginning.
otSlider.maximumValue = Float(otStack.frame.width)
otSlider.value = Float(otStack.frame.width) * 0.9
   
  
@IBOutlet weak var otWidth: NSLayoutConstraint! //Use this to set what the measurement for how wide the shape is and control it using otWidth.constant.
   
   
@IBOutlet weak var otPer: UITextField! //textField I use
   
   
@IBOutlet weak var otSlider: UISlider! //This is the slider that controls the otWidth.constant and sends the number to the otPer.

Are the sliders in a stackView ? You should have told.

Because there is automatic sizing occurring in stackViews, you may have problem to set one object width to zero.

Maybe you should consider getting the sliders out of the stackView, I don't think you will loose much…

Anyway, IMHO, making the slider disappear (zero-width) when set to zero is not a very good design for user experience.
I don't mean making the slider disappear, I mean making the shape it controls disappear because 0 is nothing, I will try your solution.
You should show some relevant code. and not only small pieces from which it is impossible to understand what's going on.

Have you connected the sliders to an IBAction ?
You should have somewhere in code:
Code Block
@IBAction func testSlider(_ sender: UISlider) {
goToZero() // That's probably where you call it
}

Accepted Answer
Alright I made a func that fixes the issue using the alpha.

Code Block
func goToZero() {
    if otSlider.value == 0 {
      otShape.alpha = 0
    } else if efSlider.value == 0 {
      efShape.alpha = 0
    } else if ifSlider.value == 0 {
      ifShape.alpha = 0
    } else {
      otShape.alpha = 1
      efShape.alpha = 1
      ifShape.alpha = 1
    }
  }

Can't set the slider to 0
 
 
Q