Skip to content

Instantly share code, notes, and snippets.

@dbolella
Created March 17, 2020 18:30
Show Gist options
  • Save dbolella/7b326fe2bd76a13576c1ec35d4132221 to your computer and use it in GitHub Desktop.
Save dbolella/7b326fe2bd76a13576c1ec35d4132221 to your computer and use it in GitHub Desktop.
SwiftUI: Lists, Navigation, and Detail Views
import SwiftUI
struct ContentView: View {
var profile: Profile = Profile(name: "Danny", subtitle: "Awesome iOS Developer", description: "Danny loves SwiftUI and thinks it's the future of iOS Development!", profilePic: "profilepic")
var profile2: Profile = Profile(name: "George", subtitle: "An OK iOS Developer", description: "George should love SwiftUI and think that it's the future of iOS Development!", profilePic: "profilepic2")
var profile3: Profile = Profile(name: "Patrick", subtitle: "Android Developer", description: "Patrick is in love with SwiftUI and wants to switch to iOS Development!", profilePic: "profilepic2")
var body: some View {
NavigationView {
List([profile, profile2, profile3]){ curProfile in
ProfileCell(profile: curProfile)
}
.navigationBarTitle("SwiftUI Team")
}
}
}
struct ProfileCell : View {
var profile : Profile
var body: some View {
NavigationLink(destination: ProfilePage(profile: profile)){
HStack{
Image(profile.profilePic)
.resizable()
.frame(width: 100, height: 100)
Text(profile.name)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
class Profile: Identifiable{
var name: String = ""
var subtitle: String = ""
var description: String = ""
var profilePic: String = ""
init(name: String, subtitle: String, description: String, profilePic: String) {
self.name = name
self.subtitle = subtitle
self.description = description
self.profilePic = profilePic
}
}
import SwiftUI
struct ProfileInformation: View {
var profile: Profile
var body: some View {
VStack{
Text(profile.name)
.font(.largeTitle)
Text(profile.subtitle)
.font(.title)
Text(profile.description)
.font(.body)
}
}
}
struct ProfilePage: View {
var profile: Profile
var body: some View {
VStack {
Image(profile.profilePic)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width:200.0)
.clipShape(Circle())
.overlay(Circle().stroke(Color.gray, lineWidth: 10))
ProfileInformation(profile: profile)
}
.padding()
}
}
struct ProfilePage_Previews: PreviewProvider {
static var previews: some View {
ProfilePage(profile: Profile(name: "Danny", subtitle: "Awesome iOS Developer", description: "Danny loves SwiftUI and thinks it's the future of iOS Development!", profilePic: "profilepic"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment