2022 SwiftUI & UI Frameworks
WWDC22 · 15 min · SwiftUI & UI Frameworks
The craft of SwiftUI API design: Progressive disclosure
Explore progressive disclosure — one of SwiftUI’s core principles — and learn how it influences the design of our APIs. We’ll show you how we use progressive disclosure, discuss how it can support quick iteration and exploration, and help you take advantage of it in your own code.
Watch at developer.apple.com ↗Code shown on screen · 21 snippets
Declaration Site Example
struct BookView: View {
let pageNumber: Int
let book: Book
init(book: Book, pageNumber: Int) {
self.book = book
self.pageNumber = pageNumber
}
var body: some View { ... }
} Call Site Example
VStack {
BookView(book: favoriteBook, page: 1)
BookView(book: savedBook, page: 234)
} Button Label
Button("Next Page") {
currentPage += 1
} Button label expanded
Button {
currentPage += 1
} label: {
Text("Next Page")
} Button label advanced case
Button {
currentPage += 1
} label: {
HStack {
Text("Next Page")
NextPagePreview()
}
} Button label common case
Button("Next Page") {
currentPage += 1
} Text example
Text("Hello WWDC22!") Stacks of Text
VStack {
Text("Hello WWDC22!")
Text("Call to Code.")
} Toolbar
.toolbar {
Button {
addItem()
} label: {
Label("Add", systemImage: "plus")
}
Button {
sort()
} label: {
Label("Sort", systemImage: "arrow.up.arrow.down")
}
Button {
openShareSheet()
}: label: {
Label("Share", systemImage: "square.and.arrow.up")
}
} Toolbar with explicit placement
.toolbar {
ToolbarItemGroup(placement: .navigationBarLeading) {
Button {
addItem()
} label: {
Label("Add", systemImage: "plus")
}
Button {
sort()
} label: {
Label("Sort", systemImage: "arrow.up.arrow.down")
}
Button {
openShareSheet()
}: label: {
Label("Share", systemImage: "square.and.arrow.up")
}
}
} Advanced use case table
var sortOrder = [KeyPathComparator(\Book.title)]
var body: some View {
Table(sortOrder: $sortOrder) {
TableColumn("Title", value: \Book.title) { book in
Text(book.title).bold()
}
TableColumn("Author", value: \Book.author) { book in
Text(book.author).italic()
}
} rows: {
Section("Favorites") {
ForEach(favorites) { book in
TableRow(book)
}
}
Section("Currently Reading") {
ForEach(currentlyReading) { book in
TableRow(book)
}
}
}
.onChange(of: sortOrder) { newValue in
favorites.sort(using: newValue)
currentlyReading.sort(using: newValue)
}
} Simpler table use case
var sortOrder = [KeyPathComparator(\Book.title)]
var body: some View {
Table(sortOrder: $sortOrder) {
TableColumn("Title", value: \Book.title) { book in
Text(book.title)
}
TableColumn("Author", value: \Book.author) { book in
Text(book.author)
}
} rows: {
ForEach(currentlyReading) { book in
TableRow(book)
}
}
.onChange(of: sortOrder) { newValue in
currentlyReading.sort(using: newValue)
}
} Table collection convenience
var sortOrder = [KeyPathComparator(\Book.title)]
var body: some View {
Table(currentlyReading, sortOrder: $sortOrder) {
TableColumn("Title", value: \.title) { book in
Text(book.title)
}
TableColumn("Author", value: \.author) { book in
Text(book.author)
}
}
.onChange(of: sortOrder) { newValue in
currentlyReading.sort(using: newValue)
}
} Table string key path convenience
var sortOrder = [KeyPathComparator(\Book.title)]
var body: some View {
Table(currentlyReading, sortOrder: $sortOrder) {
TableColumn("Title", value: \.title)
TableColumn("Author", value: \.author)
}
.onChange(of: sortOrder) { newValue in
currentlyReading.sort(using: newValue)
}
} Table without sorting
var body: some View {
Table(currentlyReading) {
TableColumn("Title", value: \.title)
TableColumn("Author", value: \.author)
}
} Stack example: leading
struct StackExample: View {
var body: some View {
HStack { // leading
Box().tint(.red)
Box().tint(.green)
Box().tint(.blue)
}
}
} Stack example: centered
struct StackExample: View {
var body: some View {
HStack { // centered
Spacer()
Box().tint(.red)
Box().tint(.green)
Box().tint(.blue)
Spacer()
}
}
} Stack example: evenly spaced
struct StackExample: View {
var body: some View {
HStack { // evenly spaced
Spacer()
Box().tint(.red)
Spacer()
Box().tint(.green)
Spacer()
Box().tint(.blue)
Spacer()
}
}
} Stack example: space only between elements
struct StackExample: View {
var body: some View {
HStack { // space only between elements
Box().tint(.red)
Spacer()
Box().tint(.green)
Spacer()
Box().tint(.blue)
}
}
} Stack example: space only before last element
struct StackExample: View {
var body: some View {
HStack { // space only before last element
Box().tint(.red)
Box().tint(.green)
Spacer()
Box().tint(.blue)
}
}
} Stack example: space only after first element
struct StackExample: View {
var body: some View {
HStack { // space only after first element
Box().tint(.red)
Spacer()
Box().tint(.green)
Box().tint(.blue)
}
}
}