Search code examples
xcodeintsliderdoubleswiftui

Multiple Slider didn't works well?


Hi I'm Korean student who make self diagnosis page of application I made self diagnosis page by slider. I need 8 slider cuz I have 8 Questions. At first I made first slider, it works well. But, I made more slider I got error message. I wanna delete error message to make my page works well. I tried everything that swift suggest. I wonder why error message appear after made second and more slider

Here is my code, I using SwiftUI, my Xcode version is 11.2.1

import SwiftUI

struct ContentView: View {
@State private var value1: Double = 0
@State private var value2: Double = 0
@State private var value3: Double = 0
@State private var value4: Double = 0
@State private var value5: Double = 0
@State private var value6: Double = 0
@State private var value7: Double = 0
@State private var value8: Double = 0
var body: some View {
    VStack {
        Text("앉아서 책(신문 잡지 서류 등)을 읽을 때")
        Slider(value: $value1,in: 0...3, step: 1).padding(.horizontal,30)
        HStack {
            Text("전혀").offset(x:17)
            Spacer()
            Text("조금").offset(x:22)
            Spacer()
            Text("상당히").offset(x:20)
            Spacer()
            Text("매우 많이")
        }.padding(.horizontal,10)
        Text("TV볼 때")
        Slider(value: $value2,in: 0...3, step: 1).padding(.horizontal,30)
        HStack {
            Text("전혀").offset(x:17)
            Spacer()
            Text("조금").offset(x:22)
            Spacer()
            Text("상당히").offset(x:20)
            Spacer()
            Text("매우 많이")
        }.padding(.horizontal,10)
        Text("공공장소(모임, 극장 등)에서 가만히 앉아 있을 때")
        Text("정차 없이 1시간 동안 운행 중인 차에 승객으로 앉아 있을 때")
        Text("오후에 주위상황이 허락되어 쉬려고 누워 있을 때")
        Text("앉아서 상대방과 이야기 할 때")
        Text("반주를 곁들이지 않은 점심식사 후 조용히 앉아 있을 때")
        Text("교통 혼잡으로 몇 분 동안 멈춰선 차 안에서")
    }
}
}

Also If I made value1 to Int, swift make error that

Initializer 'init(value:in:step:onEditingChanged:)' requires that 'Int.Stride' (aka 'Int') conform to 'BinaryFloatingPoint'

So I change

Slider(value: $value1,in: 0...3, step: 1).padding(.horizontal,30)
to
Slider(value: $value1,in: 0...3, step: 1.0).padding(.horizontal,30)

then I got new error message at

.padding(.horizontal,10)
and error message is
'Int' is not convertible to 'CGFloat?'

I just wanna know that what is the matter.

I would appreciate it if you could suggest a solution.


Solution

  • It is SwiftUI mis-error. You just have too many views in one container (top VStack in your case). It is not allowed to have more than 10, so just use Group like below (or make subview for similar pattern and extract it separately)

    struct ContentView: View {
        @State private var value1: Double = 0
        @State private var value2: Double = 0
        @State private var value3: Double = 0
        @State private var value4: Double = 0
        @State private var value5: Double = 0
        @State private var value6: Double = 0
        @State private var value7: Double = 0
        @State private var value8: Double = 0
        var body: some View {
            VStack {
                Group {
                    Text("앉아서 책(신문 잡지 서류 등)을 읽을 때")
                    Slider(value: $value1,in: 0...3, step: 1).padding(.horizontal,30)
                    HStack {
                        Text("전혀").offset(x:17)
                        Spacer()
                        Text("조금").offset(x:22)
                        Spacer()
                        Text("상당히").offset(x:20)
                        Spacer()
                        Text("매우 많이")
                    }.padding(.horizontal,10)
                }
                Group {
                    Text("TV볼 때")
                    Slider(value: $value2,in: 0...3, step: 1).padding(.horizontal,30)
                    HStack {
                        Text("전혀").offset(x:17)
                        Spacer()
                        Text("조금").offset(x:22)
                        Spacer()
                        Text("상당히").offset(x:20)
                        Spacer()
                        Text("매우 많이")
                    }.padding(.horizontal,10)
                }
    
                // ... other grouped sliders here
            }
        }
    }