[Technical Questions] #21
-
In what area do you have a technical challenge?Xcode, Simulator & Previews DescriptionHi! For some reason, the view inside Xcode is rendering my design correctly, but when I run my code, the spacing becomes very strange. ReproductionResized images and added Spacing. Code is below. //
// 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("My Favorite...", selection: $selectedSide) {
ForEach(WhichFact.allCases, id: \.self) {
Text($0.rawValue)
}
}
.pickerStyle(SegmentedPickerStyle())
.padding()
Spacer()
CappucinoView(selectedSide: selectedSide)
Spacer()
}
.navigationTitle("My Favorite...")
Spacer()
}
}
}
enum WhichFact: String, CaseIterable {
case tofu = "Evening Meal"
case place = "City in the US"
case cappucino = "Morning Drink"
var id: String {
rawValue
}
}
struct CappucinoView: View {
var selectedSide: WhichFact
var body: some View {
switch selectedSide {
case .tofu:
Bundle.module.image(fromFileNamed: "Tofu", type: "jpg")
.resizable()
.frame(width: 500, height: 500)
Spacer()
Text("...is Mapo Tofu!")
.font(.headline)
case .place:
Bundle.module.image(fromFileNamed: "Pittsburgh", type: "jpg")
.resizable()
.frame(width: 620, height: 500)
Spacer()
Text("...is Pittsburgh!")
.font(.headline)
case .cappucino:
Bundle.module.image(fromFileNamed: "Cappuccino", type: "jpg")
.resizable()
.frame(width: 400, height: 500)
Spacer()
Text("...is a cappuccino!")
.font(.headline)
}
}
}
struct TofuImageView: View {
var body: some View {
Bundle.module.image(fromFileNamed: "Tofu", type: "jpg")
.resizable()
.frame(width: 500, height: 500)
}
}
struct MoreInfoView: PreviewProvider {
static var previews: some View {
KatieLiuView()
.preferredColorScheme(.light)
}
} Expected behaviorNormal spacing Additional contextNo response Code of Conduct
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hi @katieliuu, The reason this is happening is that in your preview, you are just looking at Vishnu |
Beta Was this translation helpful? Give feedback.
Hi @katieliuu,
The reason this is happening is that in your preview, you are just looking at
KatieLiuView
, but when you navigate to that view from within the app, you're already inside aNavigationView
andKatieLiuView
nests anotherNavigationView
inside it. That is causing the extra padding to show up. If you remove theNavigationView
that is wrapping yourVStack
in the code above (you don't need it), the extra space should disappear.Vishnu