I want to display the text in Picker on Swift, but I get an error, so I want to solve it.

Asked 2 years ago, Updated 2 years ago, 50 views

Xcode12

I'm studying Swift as my first programming language.I am making a drawing application with swift.There are no errors, but I'm having trouble because I can't see the characters "color" and "line width" written in the text.Also, if you resume, everything other than the text will be displayed correctly.

Tried
I looked it up on the web and reviewed the code while looking at the reference book, but I couldn't determine if the Picker description was wrong or if the argument was wrong.I tried to change the width and height values, and I tried to use padding and Spacer spacing, but I don't know why the error occurred.
Sorry for the inconvenience, but I would appreciate it if someone could reply.

import SwiftUI

structureSettingView:View {
    @Environment(\.presentationMode) var presentationMode
    @Binding var colorSel: Int
    @Binding varlineWidth: Int
    @Binding var colors: Color

    varbody:someView {
        VStack {

            Picker(selection:$colorSel, label:Text("color").frame(width:40)){
                ForEach(0..<colors.count) {value in
                    if value == self.colors.count-1 {
                        Image(systemName: "square")
                    } else{
                        Image(systemName: "paintbrush.fill")
                            .foregroundColor (self.colors [value])
                    }
                }
            }

            Spacer()

            Picker(selection:$lineWidth, label:Text("LineWidth").frame(width:40)){
                ForEach (1..<11) {value in
                    Text(String(value))
                }
            }
            .frame (width:30)

            Spacer()

            Button(action: {
                self.presentationMode.wrappedValue.dismiss()
            }){
                Text ("Close")
            }
        }.padding()
    }
}

structureSettingView_Previews:PreviewProvider{
    static var previews:someView {
        SettingView (colorSel: .constant(0), lineWidth: .constant(3), colors: .constant([.black, .red, .blue, .green, .white]))
    }
}

swift ios xcode12

2022-09-30 19:48

1 Answers

Apple has been redesigning the UI of the components included in SwiftUI frequently, and I don't know if it's an iOS version or an Xcode issue, but it seems that since a while ago (Xcode 12.2?), Picker label: is not displayed.

SwiftUI (Xcode 12.2) Picker Label Does Not Appear

Picker Label not showing any more

SwiftUI(Xcode 12.2)Picker no longer shows label.

Unfortunately, changing the standard design of UI parts in Apple's framework (especially SwiftUI) often happens, and the tutorial screen written a few months ago cannot be reproduced exactly as it was.

I couldn't find enough articles to tell if this change was intentional by Apple, so one day it might suddenly change again, but I think it's OK if the tutorial screen doesn't reproduce as it is, taking it as a "common thing with SwiftUI."

If you really want to display words like "color" or "line width" on your screen, you will need to use VStack or HSTack to write them somewhere other than Picker as some of the above articles say.

In this area, if you want to study programming within Apple's framework, you often have to rely on not only official information but also online information, but if you describe a situation like this as an error (or as written in the text), it will be difficult to get information.

"Error" is not a general broad meaning.

  • Build Interrupted (compiled) Error with Error Message
  • Runtime error where the system detects errors after successful build and execution starts

I think it would be easier to communicate on Q&A site if you use them only (and distinguish them).


2022-09-30 19:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.