Big update
-
How do I create multi-line editable text using TextEditor
-
How do I use LazyVGrid and LazyHGrid to place views in a grid
LazyVGrid
LazyHGrid
-
How to create document-based applications using FileDocument and DocumentGroup
FileDocument
DocumentGroup
-
How do I get users to use ColorPicker to select colors
-
How do I create a Toolbar and add a button to it
NavigationView {
Text("Hello, World!").padding()
.navigationTitle("SwiftUI")
.toolbar {
ToolbarItem(placement: .bottomBar) {
Button("Press Me") {
print("Pressed")}}}}If you want to add more than one button
ToolbarItem(placement: .bottomBar) {
HStack {
Button("First") {
print("Pressed")
}
Button("Second") {
print("Pressed")}}}Copy the code
- How do I add a sidebar to iPadOS
# In iPadOS. This adds three views to split the view
struct ContentView: View {
var body: some View {
NavigationView {
Sidebar()
PrimaryView()
DetailView()
}
}
}
Copy the code
# Use 'SidebarListStyle()' if you need to display a liststruct Sidebar: View { var body: some View { List(1.. <100) { iin
Text("Row \(i)")
}
.listStyle(SidebarListStyle())
}
}
Copy the code
To improve the
- How do I use ScrollViewReader to move a scroll view to a location
struct ContentView: View {
let colors: [Color] = [.red, .green, .blue]
var body: some View {
ScrollView {
ScrollViewReader { value in
Button("Jump to #8") { value.scrollTo(8) } ForEach(0.. <10) { iin
Text("Example \(i)")
.frame(width: 300, height: 300)
.background(colors[i % colors.count])
.id(i)
}
}
}
}
}
Copy the code
- How do I display the map view
import MapKit import SwiftUI struct ContentView: View { @State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), SPAN: MKatespan (latitude delta: LongitudeDelta: 0.5) var body: some View {Map(coordinate gion:$region)}}Copy the code
- How do I use ProgressView to show the progress of a task
Struct ContentView: View {@state private var downloadAmount = 0.0letPublish (every: 0.1, on:. Main,in: .common).autoconnect()
var body: some View {
VStack {
ProgressView("Downloading...", value: downloadAmount, total: 100)
}
.onReceive(timer) { _ in
if downloadAmount < 100 {
downloadAmount += 2
}
}
}
}
Copy the code
- How to use VideoPlayer to play a video
VideoPlayer(player: AVPlayer(url: URL(string: "video.url")!)) {
VStack {
Text("Watermark"Font-size (.caption).foregroundcolor (.white).background(color.black.opacity (0.7)).clipShape(Capsule()) Spacer()}}Copy the code
- How do I use tabViewStyle() to create a scrolling page of content
struct SwiftUITestApp: App {
var body: some Scene {
WindowGroup {
TabView {
HomeView()
AccountView()
ProfileView()
SettingsView()
}
.tabViewStyle(PageTabViewStyle())
}
}
}
Copy the code
- How do I synchronize an animation from one view to another using matchedGeometryEffect()
struct ContentView: View {
@Namespace private var animation
@State private var isZoomed = false
var frame: CGFloat {
isZoomed ? 300 : 44
}
var body: some View {
VStack {
Spacer()
VStack {
HStack {
RoundedRectangle(cornerRadius: 10)
.fill(Color.blue)
.frame(width: frame, height: frame)
.padding(.top, isZoomed ? 20 : 0)
if isZoomed == false {
Text("Taylor Swift – 1989")
.matchedGeometryEffect(id: "AlbumTitle".in: animation)
.font(.headline)
Spacer()
}
}
if isZoomed == true {
Text("Taylor Swift – 1989")
.matchedGeometryEffect(id: "AlbumTitle".in: animation) .font(.headline) .padding(.bottom, 60) Spacer() } } .onTapGesture { withAnimation(.spring()) { self.isZoomed.toggle() } } .padding() .frame(maxWidth: .infinity).frame(height: isZoomed? 400:60).background(Color(white: 0.9))}}Copy the code
- How do I create an extended list
struct Bookmark: Identifiable {
let id = UUID()
let name: String
let icon: String
var items: [Bookmark]?
// some example websites
static let apple = Bookmark(name: "Apple", icon: "1.circle")
static let bbc = Bookmark(name: "BBC", icon: "square.and.pencil")
static let swift = Bookmark(name: "Swift", icon: "bolt.fill")
static let twitter = Bookmark(name: "Twitter", icon: "mic")
// some example groups
static let example1 = Bookmark(name: "Favorites", icon: "star", items: [Bookmark.apple, Bookmark.bbc, Bookmark.swift, Bookmark.twitter])
static let example2 = Bookmark(name: "Recent", icon: "timer", items: [Bookmark.apple, Bookmark.bbc, Bookmark.swift, Bookmark.twitter])
static let example3 = Bookmark(name: "Recommended", icon: "hand.thumbsup", items: [Bookmark.apple, Bookmark.bbc, Bookmark.swift, Bookmark.twitter])
}
Copy the code
struct ContentView: View {
let items: [Bookmark] = [.example1, .example2, .example3]
var body: some View {
List(items, children: \.items) { row in
Image(systemName: row.icon)
Text(row.name)
}
}
}
Copy the code
- How to use DisclosureGroup to hide and display content ‘
New property wrapper
- What is the @AppStorage property wrapper? – UserDefaults easy to read and write
SwiftUI has a special property wrapper that reads the value UserDefaults from it and automatically recalls the view’s body property when the value changes. That is, this wrapper effectively monitors the key in UserDefaults and refreshes your UI if that key changes.
struct ContentView: View {
@AppStorage("username") var username: String = "Anonymous"
var body: some View {
VStack {
Text("Welcome, \(username)!")
Button("Log in") {
self.username = "@twostraws"}}}}Copy the code
So in this case if you change the string of username UserDefaults will immediately write and update the view
UserDefaults.standard.set("@twostraws".forKey: "username") `Copy the code
Username is also updated when we change it
- What is the @ScaledMetric attribute wrapper? – Set the zoom value based on Dynamic Type
- What is the @StateObject property wrapper? – Create the reference type safely in the view
- What is a @ UIApplicationDelegateAdaptor attribute wrapper?
More and more
- How to add an AppDelegate to a SwiftUI app
- How do I continue NSUserActivity in SwiftUI
- How do I create groups and insert group lists
- How do I run some code with onChange () when state changes
- How to use ProgressView to show indeterminate progress
- How to render a full-screen mode view using fullScreenCover ()
- How do I display text and ICONS side by side using labels
- When should YOU use ContainerRelativeShape?
- How do I control which view is displayed when my application starts
- How do I recommend another application using appStoreOverlay ()
- How do I open a Web link in Safari
- How do I format dates in a text view
- How to export files using ExportFilesAction
- How to use textCase () to uppercase or lowercase TextField