SwiftUI random notes

SwiftUI random notes

Styled text

Text("Hi").bold() + Text(" there").color(.red)
This way texts with different styles can be mixed
 

Iterate with index

private func indexedStuff() -> [(Int, Thing)] { return Array(zip(viewModel.things.indices, viewModel.things)) } List(indexedStuff(), id: \.0) { index, thing in }
 

Left align text

.frame(maxWidth: .infinity, alignment: .leading)
 

iPad nav bar issue

NavigationView { Text("Hello world!") } .navigationViewStyle(StackNavigationViewStyle())
 

Image view

SwiftUI Image doesn't fit to container size by default. Needs .resizable()
.aspectRatio(contentMode: .fit)
 

Button in List

In a SwiftUI List cell, if you have a Button, tapping it will trigger the action of the entire cell instead of the button itself. To fix it, add .buttonStyle(PlainButtonStyle())
 

Long press gesture in List

In a SwiftUI List cell, doing .onLongPressGesture {} could break the scrolling. To fix that, put .onTapGesture {} right before onLongPressGesture