我是靠谱客的博主 冷艳向日葵,最近开发中收集的这篇文章主要介绍Swift Playground 中使用SwiftUI,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

MacOS: 

版本1:

import AppKit
import SwiftUI
import PlaygroundSupport

let contentView = ContentView()

PlaygroundPage.current.liveView = NSHostingController(rootView: contentView)

struct ContentView: View {
   
    var body: some View {
        
        ZStack{
            Color.black
            Text("Test Test").foregroundColor(Color.red)
        }.frame(width:200, height:200)        
    }

}

版本2: 

import AppKit
import SwiftUI
import PlaygroundSupport

private let dateFormatter: DateFormatter = {
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .medium
    dateFormatter.timeStyle = .medium
    return dateFormatter
}()

struct ContentView: View {
    @State private var dates = [Date]()
   
    var body: some View {
        NavigationView {
            MasterView(dates: $dates)
            DetailView()
        }.navigationViewStyle(DoubleColumnNavigationViewStyle())
    }
}

struct MasterView: View {
    @Binding var dates: [Date]
   
    var body: some View {
        List {
            ForEach(dates, id: .self) { date in
                NavigationLink(
                    destination: DetailView(selectedDate: date)
                ) {
                    Text("(date, formatter: dateFormatter)")
                }
            }.onDelete { indices in
                indices.forEach { self.dates.remove(at: $0) }
            }
        }
    }
}

struct DetailView: View {
    var selectedDate: Date?
   
    var body: some View {
        Group {
            if selectedDate != nil {
                Text("(selectedDate!, formatter: dateFormatter)")
            } else {
                Text("Detail view content goes here")
            }
        }
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

let contentView = ContentView()

PlaygroundPage.current.liveView = NSHostingController(rootView: contentView)

iOS:

import UIKit
import SwiftUI
import PlaygroundSupport

private let dateFormatter: DateFormatter = {
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .medium
    dateFormatter.timeStyle = .medium
    return dateFormatter
}()

struct ContentView: View {
    @State private var dates = [Date]()
   
    var body: some View {
        NavigationView {
            MasterView(dates: $dates)
                .navigationBarTitle(Text("Master"))
                .navigationBarItems(
                    leading: EditButton(),
                    trailing: Button(
                        action: {
                            withAnimation { self.dates.insert(Date(), at: 0) }
                    }
                    ) {
                        Image(systemName: "plus")
                    }
            )
            DetailView()
        }.navigationViewStyle(DoubleColumnNavigationViewStyle())
    }
}

struct MasterView: View {
    @Binding var dates: [Date]
   
    var body: some View {
        List {
            ForEach(dates, id: .self) { date in
                NavigationLink(
                    destination: DetailView(selectedDate: date)
                ) {
                    Text("(date, formatter: dateFormatter)")
                }
            }.onDelete { indices in
                indices.forEach { self.dates.remove(at: $0) }
            }
        }
    }
}

struct DetailView: View {
    var selectedDate: Date?
   
    var body: some View {
        Group {
            if selectedDate != nil {
                Text("(selectedDate!, formatter: dateFormatter)")
            } else {
                Text("Detail view content goes here")
            }
        }.navigationBarTitle(Text("Detail"))
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

let contentView = ContentView()

PlaygroundPage.current.liveView = UIHostingController(rootView: contentView)

最后

以上就是冷艳向日葵为你收集整理的Swift Playground 中使用SwiftUI的全部内容,希望文章能够帮你解决Swift Playground 中使用SwiftUI所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(36)

评论列表共有 0 条评论

立即
投稿
返回
顶部