How to link two min max sliders with swiftui iOS15

0 votes

I have two sliders to filter the results. One of the sliders sets minimum value another sets maximum value. Filters work properly, except min value can be increased to a value more than max and max can be lowered below min.

How can I link these sliders so that min does not exceed max and max does not go below min?

Here is the code:

@State private var minimum: Double = 98.0
@State private var maximum: Double = 1000000.0

var filteredProducts: [Product]

var body: some View {
    List {
        Section(header: Text("Filters")){
            DisclosureGroup("Price range") {
                Slider(value: $minimum, in: 98...1000000) {
                    Text("\(minimum)")
                } minimumValueLabel: {
                    Text("min")
                } maximumValueLabel: {
                    Text("\(minimum, specifier: "%.0f")")
                }
                
                Slider(value: $maximum, in: 98...1000000) {
                    Text("\(maximum)")
                } minimumValueLabel: {
                    Text("max")
                } maximumValueLabel: {
                    Text("\(maximum, specifier: "%.0f")")
                }
            }
        }
    }
}

var slideresults: [Product] {
    if minimum == 0 && maximum == 1000000000.0 {
        return filteredProducts
    } else {
        return filteredProducts.filter {
            $0.price > minimum &&
            $0.price < maximum
        }
    }
}


 

Sep 22, 2022 in Others by gaurav
• 23,260 points
1,208 views

1 answer to this question.

0 votes

I see you asked it a long time ago, hope still this can help.

I had the same problem and this is how I solved it. (showing on your code)

@State private var minimum: Double = 98.0
@State private var maximum: Double = 1000000.0

var filteredProducts: [Product]

var body: some View {
    List {
        Section(header: Text("Filters")){
            DisclosureGroup("Price range") {
                Slider(value: $minimum, in: 98...maximum) {
                    Text("\(minimum)")
                } minimumValueLabel: {
                    Text("min")
                } maximumValueLabel: {
                    Text("\(maximum, specifier: "%.0f")")
                }
                
                Slider(value: $maximum, in: minimum...1000000) {
                    Text("\(maximum)")
                } minimumValueLabel: {
                    Text("\(minimum, specifier: "%.0f")
                } maximumValueLabel: {
                    Text("\(maximum, specifier: "%.0f")")
                }
            }
        }
    }
}

Basically, give your Min value Slider your @state max value and vice versa for the Max value Slider. And put corresponding labels: max value label to min value slider and vice versa.

answered Sep 22, 2022 by rajatha
• 7,680 points

Related Questions In Others

+1 vote
0 answers

How to split a number with coma betweeen two colunms

Jul 3, 2019 in Others by anonymous
877 views
0 votes
1 answer

How to merge two cells in excel with same field name

Insert 2 new columns, G & H. Enter ...READ MORE

answered Oct 7, 2022 in Others by narikkadan
• 63,600 points
1,631 views
0 votes
1 answer

How to link two excel sheet in same excel file

It would be best if you made ...READ MORE

answered Jan 29, 2023 in Others by narikkadan
• 63,600 points
460 views
0 votes
1 answer
0 votes
2 answers

In List of Dicts, find min() value of a common Dict field

lst = [{'price': 99, 'barcode': '2342355'}, {'price': ...READ MORE

answered Aug 31, 2018 in Python by Omkar
• 69,220 points
7,936 views
0 votes
0 answers

Use of min and max functions in C++

Are std::min and std::max better than fmin ...READ MORE

Jun 2, 2022 in C++ by Nicholas
• 7,760 points
595 views
0 votes
1 answer

Use of min and max functions in C++

The functions fmin and fmax are designed ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
11,336 views
0 votes
0 answers

How can I get the maximum or minimum value in a vector?

In C++, how can I find the greatest or minimum value in a vector? Is it correct to assume that it would be similar with an array? Do I require an iterator?  I tried max element, but I kept receiving errors. vector<int>::const_iterator it; it = max_element(cloud.begin(), cloud.end()); error: request for ...READ MORE

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
587 views
0 votes
1 answer

How to select random value from iOS picker wheel using Selenium

Two ways to do this : Randomise the UIAPickerWheel[1] index ...READ MORE

answered Sep 21, 2022 in Others by rajatha
• 7,680 points
1,081 views
0 votes
1 answer

Is there a way to search the iOS app store online?

Apple finally made it possible to just use ...READ MORE

answered Sep 21, 2022 in Others by rajatha
• 7,680 points
3,977 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP