Autolayout Slider Issues

2 Issues with my sliders. The one that belongs to the green shape controls the height of the green shape and the red shape (it makes the red shape go down instead of change the height). The one next to the red shape makes the red shape get smaller and bigger in the opposite direction I want. How do I fix these Issues with my constraints? Is it because of the slider constraints or the constraints with my shapes.

Controlling the height of the green shape makes the red shape go down.
Screen Shot 2020-08-19 at 12.51.42 AM

I think we need to see your Storyboard layout and also we need to know which edge of each of the coloured views is the edge that is anchored (top or bottom or left or right depending on the direction of movement) meaning the opposite edge is the one that is under control of the slider.

In each case the slider is setting the constant value for the moveable edge so to make the view smaller, the constant is being increased in value pushing it away from the opposite edge that is anchored. Does that make sense?

The bottom of the red view is also being controlled and my sliders are vertical. I don’t understand how the constant of 1 shape is pushing the other. The constant of the green one affects the whole red shape.

This is what my storyboard looks like.

This screen shot still does not show the structure of your storyboard and how the elements are arranged within the stackView (or stackViews).

What you need to include is your Document Outline view in association with the storyboard View you have shown above. It’s important to know how the individual elements are arranged, be they a slider or a view.

This is my document outline

Placing all the elements inside a single container will result in one affecting the other as you have already discovered. Each view and it’s associated slider need to be in there own view container which itself has constraints that dictate its size. Those elements can then change size independently.

Okay I will try a container view. Btw I really appreciate you helping me thank you.

So I have 4 container views that I linked back to my original view controller and I made new outlets for the view, the slider, and the height but I have nil values when I run it. What seems to be the issue?

I have the elements in their own container and they work but I want to calculate ROA which requires all 4 of those elements from 4 different containers. How would I go about doing that?

OK so what you are saying is that each of those sliders not only control the view height but they also adjust a value you are using to perform a calculation. So the sliders need to be calibrated to the range of values you are using in your ROA calculation but the view size in pixels (or points) need to be a multiplier of that value based on the maximum height of the view.

As an example lets say that one of the values (let’s call it X) was between 0 and 10 and your view height ranges from 0 to 250 points. So the multiplier in that case is 25 which means that for every 1 of X the view height would be 25 points.

The problem is that the multiplier has to be calculated at the time depending on the device you are using. The iPhone 11 Pro Max will have a different screen resolution to an iPhone XS or and iPhone SE so catering for that is tricky.

I have a method Roa() that takes the different slider values and calculates it but it’s in my parent vc and I moved my slider values to my child vcs. i’m not sure how to calculate it.

Somehow you need to get those values back from the ChildVC to the ParentVC. The most common way is to use a Protocol & Delegate method.

Okay I did that and I have the values in my parentVC and I am using a func called calROA() to calculate a number using those values. Is there any way I can take my calROA() method from my parent to my childvcs? (I don’t know if you can transfer a method between 2 viewcontrollers)

Do you mean move the code from the parentVC to the childVC and do the calculation in the ChildVC and then pass the result of the calculation back to the parentVC via the protocol?

If that is a yes then of course you can. You can put your code where you like if it makes sense to do so.

I mean pass from the parent to the child because my children have the sliders, I can’t seem to pass a method but I’m trying to pass roaLabel.text = String(format: “%.1f%%”, perAns).

What method are you using to instantiate or present your childVC?

This is the method I want to transfer. My revNum, costNum, fixANum and curANum are values I transferred over from the child using protocols.

func calROA() {
        let ans = Double(revNum - costNum)/Double(fixANum + curANum)
        let perAns = ans * 100
        print("this is the value for revenue " + "\(revNum)")
        print("this is the value for costs " + "\(costNum)")
        print("this is the value for fixed assets " + "\(fixANum)")
        print("this is the value for current assets " + "\(curANum)")
        print("this is the roa " + "\(perAns)")
        roaLabel.text = String(format: "%.1f%%", perAns)
        roaValue = roaLabel.text!
    }