[Technical Questions] #14
-
In what area do you have a technical challenge?Xcode, Simulator & Previews DescriptionI am trying to add images into my UI, and have added them in the "Media" folder already. However, I cannot reference them for some reason. I call the Image() function, but nothing seems to show up. In addition, I'm getting a "File Types Order Violation". ReproductionI am calling Images in my switch statements, but they don't seem to show up. I provided some of the code I wrote - but let me know if you'd like me to remove it from this post! Thanks. //
// File.swift
//
//
// Created by Katie Liu on 1/24/23.
//
import Foundation
import SwiftUI
struct KatieLiuView: View {
init() {
UISegmentedControl.appearance().selectedSegmentTintColor = .brown
UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.white], for: .selected)
}
@State private var selectedSide: WhichFact = .tofu
var body: some View {
NavigationView {
VStack {
Picker("Food and Fall", selection: $selectedSide) {
ForEach(WhichFact.allCases, id: \.self) {
Text($0.rawValue)
}
}
.pickerStyle(SegmentedPickerStyle())
.padding()
Spacer()
CappucinoView(selectedSide: selectedSide)
Spacer()
}
.navigationTitle("Food and Fall")
}
}
}
enum WhichFact: String, CaseIterable {
case tofu = "Evening Snack"
case place = "Home"
case cappucino = "Mornin GoTo"
var id: String {
rawValue
}
}
struct CappucinoView: View {
var selectedSide: WhichFact
var body: some View {
switch selectedSide {
case .tofu:
Image("Tofu")
case .place:
Image("Pittsburgh")
case .cappucino:
Image("Cappucino")
}
}
}
struct TofuImageView: View {
var body: some View {
Image("Tofu")
.resizable()
.frame(width: 250, height: 400)
.shadow(color: .white, radius: 100)
}
}
struct MoreInfoView: PreviewProvider {
static var previews: some View {
KatieLiuView()
.preferredColorScheme(.light)
}
} Expected behaviorThe image is supposed to be displayed under the segmented menu. Additional contextNo response Code of Conduct
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hi @katieliuu, Thank you for your question! import InstructorViews
// ...
struct CappucinoView: View {
var selectedSide: WhichFact
var body: some View {
switch selectedSide {
case .tofu:
Bundle.module.image(fromFileNamed: "Tofu", type: "jpeg"))
case .place:
Bundle.module.image(fromFileNamed: "Tofu", type: "jpeg"))
case .cappucino:
Bundle.module.image(fromFileNamed: "Tofu", type: "jpeg"))
}
}
} You will need to adapt the Let us know if this helps you and solves your problem! |
Beta Was this translation helpful? Give feedback.
-
The problem with this was that I was housing images in "Media" and not "Resources" - Resources is the folder I defined in my code in the main Package to hold all resources for the target "Katie Liu". Therefore, simply moving the images over to Resources solved the problem. |
Beta Was this translation helpful? Give feedback.
The problem with this was that I was housing images in "Media" and not "Resources" - Resources is the folder I defined in my code in the main Package to hold all resources for the target "Katie Liu". Therefore, simply moving the images over to Resources solved the problem.