【问题标题】:Xcode Playgrounds liveview not working, code from swift playgroundsXcode Playgrounds liveview 无法正常工作,来自 Swift Playgrounds 的代码
【发布时间】:2021-09-25 22:00:49
【问题描述】:

我想建立一个 Playground 来存储我的笔记,以便我使用 Xcode Playgrounds 或仅使用 Swift Playgrounds 进行下一次考试。但是,我尝试在 Playgrounds 中构建一个带有箭头的列表以用于导航,但是在搜索大量教程后缺乏信息,我无法在其上创建导航列表 斯威夫特游乐场。所以我切换到 Xcode 游乐场。但我似乎无法加载 LiveView。

注意:没有显示错误。 Xcode Playground 显示“Successful”,但没有显示任何内容。

import SwiftUI
import PlaygroundSupport

//--------------------------------------------------------------//
// Weighted Asesstment III Notes

// Subject: Science
// Written in: English & SwiftUI

struct IntroView: View {
    // arrays cus I wanna put this to a list
    let expTech = ["Chromatography","Evaporation", "Distillation", "Dissolving","Magnetic Attraction", "Filtration"]
    
    let expUse = ["Chromatography",
                  "Method: Paper",
                  "Used for: ",
                  "Evaporation",
                  "Method: Heating",
                  "Used for: Separating salt & water",
                  "Distillation",
                  "Method: Heating of liquid mixture with different boiling points",
                  "eg. Used for: Obtaining pure water from alcohol",
                  "Dissolving",
                  "Method",
                  "Used for",
                  "Magnetic Attraction",
                  //I need complete silence lol
                  //Iron nickel cobalt steel
                  "Method: Magnets (I,N,C,S)",
                  "Used for: Separating magnetic objects from non magnets",
                  "Filtration",
                  "Method: Filtering/Use of filter paper",
                  "Used for: Separating mixture of insoluble solid from liquid"]
    
    var body: some View {
            // ZStack
            ZStack {
                // background color
                Color.white
                // VStack
                VStack {
                    LinearGradient(gradient: Gradient(colors: [.white, .gray, .black]), startPoint: .leading, endPoint: .trailing)
                        .mask(Text("WA 3 Sci Notes: M5C7")
                                .font(.system(size: 30, weight: .bold, design: .monospaced)))
                        .foregroundColor(Color.black)
                        .offset(x: 10, y: -325)
                        .padding()
                    Text("Types of experimental techniques")
                        .foregroundColor(Color.black)
                        .offset(x: -100, y: -710)
                        .font(.system(size: 25, weight: .semibold))
                }
                ZStack {
                    Rectangle()
                        .frame(width: 340, height: 240)
                        .cornerRadius(20)
                        .scaledToFill()
                        .shadow(color: Color.black, radius: 10)
                        .offset(x: -100, y: -120)
                        // list
                    List(expTech, id: \.self) { expTech in Text(expTech) }
                        .frame(width: 350, height: 250, alignment: .center)
                        .cornerRadius(25)
                        .offset(x: -100, y: -120)
                }
                
                    Text("Uses of Experimental Techniques")
                        .foregroundColor(Color.blue)
                        .offset(x: 80, y: 40)
                        .font(.system(size: 25, weight: .semibold))
                
                ZStack {
                    VStack {
                        Rectangle()
                            .frame(width: 600, height: 250)
                            .cornerRadius(20)
                            .scaledToFill()
                            .shadow(color: Color.black, radius: 5)
                            .offset(x: 5, y: 530)
                        
                        
                        }
                    }
                    
                }
            }
    }

PlaygroundPage.current.setLiveView(IntroView())

【问题讨论】:

  • 我遇到了同样的问题,看起来List 不适用于SwiftUI playground

标签: swiftui swift-playground


【解决方案1】:

问题可能是您的视图无法正确呈现。我将您的代码放入 Xcode 项目(不是情节提要)中,它看起来像这样:

你甚至可以在操场上看到它奇怪的渲染:

【讨论】:

  • 啊。我没有使用自动布局。但我不是在 Xcode 上运行,而是在 Xcode Playgrounds 上运行。
  • 我知道。我使用 Xcode 至少看到了视图的样子。我不知道你所说的自动布局是什么意思。
  • 我的错。我忘记了 SwiftUI 不像 UIKit 那样使用自动布局。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-09
  • 1970-01-01
  • 1970-01-01
  • 2021-09-26
  • 2022-01-24
  • 1970-01-01
相关资源
最近更新 更多