https://github.com/cuadros-code/project-8-swiftui-moonshot
MoonShot project
https://github.com/cuadros-code/project-8-swiftui-moonshot
dateformatter extension generics griditem lazyvgrid lazyvstack navigationlink
Last synced: 3 months ago
JSON representation
MoonShot project
- Host: GitHub
- URL: https://github.com/cuadros-code/project-8-swiftui-moonshot
- Owner: cuadros-code
- Created: 2024-11-08T21:24:56.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-12-05T20:01:22.000Z (5 months ago)
- Last Synced: 2024-12-05T21:18:18.227Z (5 months ago)
- Topics: dateformatter, extension, generics, griditem, lazyvgrid, lazyvstack, navigationlink
- Language: Swift
- Homepage:
- Size: 52.3 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Moonshot
- LazyVStack
- NavigationLink
- LazyVGrid
- GridItem
- Generics
- extension
- DateFormatter### Resizing images to fit the available space
```swift
Image(.colombia)
.resizable()
.scaledToFit()
.containerRelativeFrame(.horizontal) { size, axis in
size * 0.8
}
```
---### How ScrollView lets us work with scrolling data
```swift
import SwiftUIstruct CustomText: View {
let text: String
var body: some View {
Text(text)
}
init(text: String) {
print("Creating a CustomText")
self.text = text
}
}struct ContentView: View {
var body: some View {
ScrollView {
LazyVStack(spacing: 10) {
ForEach(0..<100) {
CustomText( text: "Item \($0)")
.font(.title)
}
}
.frame(maxWidth: .infinity)
}
}
}
```
---### Pushing new views onto the stack using NavigationLink
```swift
import SwiftUIstruct ContentView: View {
var body: some View {
NavigationStack {
List(0..<10) { row in
NavigationLink("Row \(row)") {
Text("Detail \(row)")
}
}
NavigationLink {
Text("Detail View")
} label: {
VStack {
Text("This is a label")
Text("So is this")
Image(systemName: "face.smiling")
}
}
.navigationTitle("SwiftUI")
}
}
}
```
---### Working with hierarchical Codable data
```swift
import SwiftUIstruct User: Codable {
let name: String
let address: Address
}struct Address: Codable {
let street: String
let city: String
}struct ContentView: View {
var body: some View {
Button("Decode JSON") {
let input = """
{
"name": "Taylor Swift",
"address": {
"street": "555, Taylor Swift Avenue",
"city": "Nashville"
}
}
"""
let data = Data(input.utf8)
if let user = try? JSONDecoder().decode(User.self, from: data) {
print(user.address.street)
}
}
}
}
```
---### How to lay out views in a scrolling grid
```swift
import SwiftUIstruct ContentView: View {
let layout = [
GridItem(.adaptive(minimum: 80, maximum: 120))
// GridItem(.fixed(80)),
// GridItem(.fixed(80)),
// GridItem(.fixed(80)),
]
var body: some View {
ScrollView {
LazyVGrid(columns: layout) {
ForEach(0..<1000) {
Text("Item \($0)")
}
}
}
}
}
```