Provide views, controls, and layout structures for declaring your app's user interface using SwiftUI.

SwiftUI Documentation

Posts under SwiftUI tag

2,332 Posts
Sort by:
Post not yet marked as solved
1 Replies
134 Views
Hello everyone My goal is to create Apple's activity ring sparkle effect. So I found Paul Hudson's Vortex library. There is already a sparkle effect, but I don't know how to modify it to achieve my goal. Because I'm pretty new to SwiftUI animations. Does anyone have any idea how I could do this? Vortex project: https://github.com/twostraws/Vortex
Posted
by iRIG.
Last updated
.
Post not yet marked as solved
4 Replies
580 Views
Hi, My app launches with a mixed immersive space. the Preferred Default Scene Session Role is set to Immersive Space Application Session Role. ImmersiveSpace(id: "sceneSpace"){ ImmersiveView() .environmentObject(modelObject) }.immersionStyle(selection: .constant(.mixed), in: .mixed) Other WindowGroups are opened too. Problem: When the x button (bottom left corner) is tapped on any WindowGroup the immersive space is dismissed. When the user opens the app again the immersive space is gone. The same happens when the user opens the Home Screen. How can I keep the same immersive space when the app is opened again. Thank you!
Posted
by gebs.
Last updated
.
Post not yet marked as solved
0 Replies
82 Views
I'm developing an app with a chart in SwiftUI. I want the following block of code to run when the chart is clicked. On my personal iPhone, the app works flawlessly. But when I try it in the simulator it crashes and gives me about 5-10 of the following errors. When I remove the @Query macro from the code block, the application does not crash in the simulator, but I continue to get the errors I mentioned. If I do not run the following code block, I do not get the errors I mentioned. struct SaleDetailView: View { @Query(filter: #Predicate<Registration> { !$0.activeRegistration }) private var regs: [Registration] var body: some View { VStack { DailySaleView() } .padding() } } Thank you in advance for your answers. Do not hesitate to ask if you have any questions. Thanks, MFS
Posted Last updated
.
Post not yet marked as solved
0 Replies
75 Views
Hi, guys. I am preparing to develop a Vision Pro app with Unity. The Play to Device, which connects Unity Engine and Vision Pro, worked well, and there was no problem with the connection with Vision Pro simulator. But when I tried to connect Xcode and Vision Pro, I couldn't see Vision Pro itself in the device list. (The iPhone 11, which was wired as a test, recognizes well.) I looked up the forum and it was simple to connect. The link to the post I found is below. https://forums.developer.apple.com/forums/thread/746464 I don't know why it's not working even though I look up YouTube. Leaving my work environment, I'd appreciate it if you could leave a helpful answer. MacBook : M2 MacBook Xcode Ver. : 15.3 VisionPro Ver. : 1.1.2 Developer accounts: All use the same Apple developer account
Posted Last updated
.
Post not yet marked as solved
0 Replies
98 Views
Hello, fairly new to Swift, I come from a React Native background. One of the hardest things I'm finding is simply customising the screen headers in the navigation. I've managed to do it using a custom modifier that uses .toolbar and ToolbarItem as shown below: struct NavBar: ViewModifier { let title: String let showBackButton: Bool? @Environment(\.dismiss) private var dismiss func body(content: Content) -> some View { return content .toolbar { if showBackButton == true { ToolbarItemGroup(placement: .navigationBarLeading) { Button(action: { dismiss() }) { Image("BackButton") }.padding(.top, 18) } } ToolbarItem(placement: .principal) { Text(title) .font(Font.custom("Knight Vision", size: 28)) .foregroundColor(.white).padding(.top, 20) } } .navigationBarBackButtonHidden(true) } } This is all fine and suits my needs however I'm finding that the toolbar does not slide in with the rest of the screen when navigating to as screen with it on. I would expect the title to slide in with the other items on the screen. Especially since the toolbar does animate out, it just does not animate in. Heres a video so you can see what I mean. Am I doing something wrong here? Is there a better way to do this? [linkText](https://www.youtube.com/shorts/6M-glapBZz0 /)
Posted
by ejjsharpe.
Last updated
.
Post not yet marked as solved
0 Replies
110 Views
UPDATED: I determined the line causing the hang was .animation(.default, value: layout). I'm seeing a strange issue when switching between a ScrollView/LazyVGrid and a List with my SwiftData but when toggling the layout it ends up freezing and I can't confirm what's causing the app to hang since there's no crash. I'm not getting much info from the debugger. Any help would be appreciated. struct ContentView: View { @Environment(\.modelContext) private var modelContext @Query private var items: [Item] let gridItemLayout = [ GridItem(.adaptive(minimum: 150))] @State private var layout = Layout.grid var body: some View { NavigationStack { ZStack { if layout == .grid { ScrollView { LazyVGrid(columns: gridItemLayout, spacing: 5) { ForEach(items) { item in } } } } else { List { ForEach(items) { item in } } } } // MARK: HERE'S THE ERROR .animation(.default, value: layout) .navigationTitle("ScrollView") .toolbar { ToolbarItemGroup { Button(action: addItem) { Label("Add Item", systemImage: "plus") } Menu { Picker("Layout", selection: $layout) { ForEach(Layout.allCases) { option in Label(option.title, systemImage: option.imageName) .tag(option) } } .pickerStyle(.inline) } label: { Label("Layout Options", systemImage: layout.imageName) .labelStyle(.iconOnly) } } } } } } @Model public class Item: Codable { public let id: String public enum CodingKeys: String, CodingKey { case id } public init(id: String) { self.id = id } required public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) id = try container.decode(String.self, forKey: .id) } public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encode(id, forKey: .id) } }
Posted Last updated
.
Post not yet marked as solved
9 Replies
2.9k Views
I tried animating the scrollTo() like so, as described in the docs. - https://developer.apple.com/documentation/swiftui/scrollviewreader swift withAnimation { scrollProxy.scrollTo(index, anchor: .center) } the result is the same as if I do swift withAnimation(Animation.easeIn(duration: 20)) {     scrollProxy.scrollTo(progress.currentIndex, anchor: .center) } I tried this using the example from the ScrollViewReader docs. With the result that up and down scrolling has exactly the same animation. struct ScrollingView: View {     @Namespace var topID     @Namespace var bottomID     var body: some View {         ScrollViewReader { proxy in             ScrollView {                 Button("Scroll to Bottom") {                     withAnimation {                         proxy.scrollTo(bottomID)                     }                 }                 .id(topID)                 VStack(spacing: 0) {                     ForEach(0..100) { i in                         color(fraction: Double(i) / 100)                             .frame(height: 32)                     }                 }                 Button("Top") {                     withAnimation(Animation.linear(duration: 20)) {                         proxy.scrollTo(topID)                     }                 }                 .id(bottomID)             }         }     }     func color(fraction: Double) - Color {         Color(red: fraction, green: 1 - fraction, blue: 0.5)     } } struct ScrollingView_Previews: PreviewProvider {     static var previews: some View {         ScrollingView()     } }
Posted
by Bersaelor.
Last updated
.
Post not yet marked as solved
2 Replies
96 Views
It seems that when you tested the official code today, you found that screen reading works fine with English characters, but when using Chinese characters, the Text and Button elements cannot be read correctly.. It's important to address this issue to bring convenience to your blind friends. The official code address is:https://developer.apple.com/documentation/swiftui/creating_accessible_views.
Posted
by Dast1.
Last updated
.
Post not yet marked as solved
3 Replies
113 Views
I have added a button in UIController: let button = UIButton(type: .custom) button.setTitle("test", for: .normal) button.addTarget(self, action: #selector(addToSiri), for: .touchUpInside) self.view.addSubview(button) Then using it in SwiftUI HStack { Controller() }.frame(maxWidth:.infinity, idealHeight: 60) And then using swiftUI in UIHostingController: let vc = UIHostingController(rootView: SwiftUIView()) But when I tap the button, the addToSiri is never called. I don't know why.
Posted
by WWWang.
Last updated
.
Post not yet marked as solved
1 Replies
117 Views
Hi everyone, I’m just starting with swift and Xcode and have a basic question. I have the following code I found online for an app that generates math addition questions. I would like to run this Math app on my iPhone just before I open the apps I use most often (let’s say mail, WhatsApp, calendar and notes) ask me a maths question and if I answer correctly, carryon with the app originally intended to be opened. I can do the opening of the Math app before the apps I use more often with shortcuts. I would like to modify the code bellow so that if I answer correctly it “closes” itself and returns to the originally intended app. With that intention I included the “exit(0)”, but I get an error. Thanks for your help in advance! Best, Tom struct ContentView: View { @State private var correctAnswer = 0 @State private var choiceArray : [Int] = [0, 1, 2, 3] @State private var firstNumber = 0 @State private var secondNumber = 0 @State private var difficulty = 1000 var body: some View { VStack { Text("(firstNumber) + (secondNumber)") .font(.largeTitle) .bold() HStack { ForEach(0..<2) {index in Button { answerIsCorrect(answer: choiceArray[index]) generateAnswers() } label: { AnswerButton(number: choiceArray[index]) } } } HStack { ForEach(2..<4) {index in Button { answerIsCorrect(answer: choiceArray[index]) generateAnswers() } label: { AnswerButton(number: choiceArray[index]) } } } } func answerIsCorrect(answer: Int){ if answer == correctAnswer {exit(0)} } } func generateAnswers(){ firstNumber = Int.random(in: 0...(difficulty/2)) secondNumber = Int.random(in: 0...(difficulty/2)) var answerList = Int correctAnswer = firstNumber + secondNumber for _ in 0...2 { answerList.append(Int.random(in: 0...difficulty)) } answerList.append(correctAnswer) choiceArray = answerList.shuffled() } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
Posted
by txdj.
Last updated
.
Post not yet marked as solved
2 Replies
173 Views
Has anyone successfully persisted Color, particularly in SwiftData? So far my attempts have failed: Making Color conform to Codable results in a run time error (from memory something about ColorBox). Color.Resolved already conforms Codable but this results in "SwiftData/ModelCoders.swift:124: Fatal error: Composite Coder only supports Keyed Container" None of the other color types conform to Codable (CGColor, NSColor and UIColor) so does the swift language really not have a persistable color type?
Posted
by Uasmel.
Last updated
.
Post not yet marked as solved
0 Replies
131 Views
After a while I returned to Xcode . I bought a new Mac mini M2 and I installed the Xcode 15.3. To familiarize with the changes in the plataform I follow the tutorial you provide. I create a new iOS app following the instructions. I clicked in the ContentView and I was supposed to see in the canvas a representation of an iPhone running my app. Insteady I saw an error message "Cannot preview in this file - Unexpected error ocurred". I'm blocked in this situation . Do you have a solution for that? The code is provided by your tutorial. I didn't change it. Thanks for your help in this matter. Fernando
Posted
by araferna.
Last updated
.
Post not yet marked as solved
2 Replies
109 Views
Is there a system deep link URI to the built in files app? I would like to direct users to my apps location in the files app. For example files://myApp The only exposed deep links for system I can find are the ones for mail, sms, FaceTime etc. Thank you (tag used for post was because I couldn’t find a deep link tag)
Posted
by RyanTCB.
Last updated
.
Post not yet marked as solved
0 Replies
108 Views
Hello everyone, I looked at various methods how to Unit/UITest SwiftData but I couldn't find something simple. Is it even possible to test SwiftData? Does someone found a solution for that?
Posted
by iRIG.
Last updated
.
Post not yet marked as solved
1 Replies
562 Views
The code for @State doesn't seem to work. struct DonutListView: View { var donutList: DonutList @State private var donutToAdd: Donut? var body: some View { List(donutList.donuts) { DonutView(donut: $0) } Button("Add Donut") { donutToAdd = Donut() } .sheet(item: $donutToAdd) { // <-- would need a "donut in" TextField("Name", text: $donutToAdd.name) // <-- donutToAdd is optional and I'm not sure how it would be unwrapped Button("Save") { donutList.donuts.append(donutToAdd) donutToAdd = nil } Button("Cancel") { donutToAdd = nil } } } } Does anyone have a fix for this? Thanks, Dan!
Posted
by dan101.
Last updated
.
Post not yet marked as solved
0 Replies
93 Views
Hi, I have a List and I want to limit the dynamic text size for some of the elements in the list's row item view. I created a test view below. The ".dynamicTypeSize(.large)" restriction only works if it's applied to the List view, not if it's set for the the ContentItemView in the ForEach below. Is there a reason for this? Do I need to do something else to limit a list row to a certain size? The example only has a text field, but I want to do this for a Image with some text inside it, and I wanted to restrict that text field, but it doesn't seem to work when the view is inside a List row. Please let me know if there's a workaround for it. import SwiftUI import CoreData struct ContentView: View { @FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)], animation: .default) private var items: FetchedResults<Item> @State private var multiSelectedContacts = Set<Item.ID>() var body: some View { NavigationStack { List (selection: $multiSelectedContacts) { ForEach(items) { item in ContentItemView(item: item) } .dynamicTypeSize(.large) // <-- doesn't works } .dynamicTypeSize(.large) // <-- THIS WORKS } } } struct ContentItemView: View { @Environment(\.managedObjectContext) private var viewContext @ObservedObject var item: Item @State var presentConfirmation = false var body: some View { HStack { if let timestamp = item.timestamp, let itemNumber = item.itemNumber { Text("\(itemNumber) - \(timestamp, formatter: itemFormatter)") } } .popover(isPresented: $item.canShowPopover, content: { Text("Test Item Label") .frame(width: 100, height: 150) }) } } private let itemFormatter: DateFormatter = { let formatter = DateFormatter() formatter.dateStyle = .short formatter.timeStyle = .long return formatter }() #Preview { ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) }
Posted
by zulfishah.
Last updated
.
Post marked as solved
2 Replies
148 Views
Hi team, I'm running into the following issue, for which I don't seem to find a good solution. I would like to be able to drag and drop items from a view into empty space to open a new window that displays detailed information about this item. Now, I know something similar has been flagged already in this post (FB13545880: Support drag and drop to create a new window on visionOS) HOWEVER, all this does, is launch the App again with the SAME WindowGroup and display ContentView in a different state (show a selected product e.g.). What I would like to do, is instead launch ONLY the new WindowGroup, without a new instance of ContentView. This is the closest I got so far. It opens the desired window, but in addition it also displays the ContentView WindowGroup WindowGroup { ContentView() .onContinueUserActivity(Activity.openWindow, perform: handleOpenDetail) } WindowGroup(id: "Detail View", for: Reminder.ID.self) { $reminderId in ReminderDetailView(reminderId: reminderId! ) } .onDrag({ let userActivity = NSUserActivity(activityType: Activity.openWindow) let localizedString = NSLocalizedString("DroppedReminterTitle", comment: "Activity title with reminder name") userActivity.title = String(format: localizedString, reminder.title) userActivity.targetContentIdentifier = "\(reminder.id)" try? userActivity.setTypedPayload(reminder.id) // When setting the identifier let encoder = JSONEncoder() if let jsonData = try? encoder.encode(reminder.persistentModelID), let jsonString = String(data: jsonData, encoding: .utf8) { userActivity.userInfo = ["id": jsonString] } return NSItemProvider(object: userActivity) }) func handleOpenDetail(_ userActivity: NSUserActivity) { guard let idString = userActivity.userInfo?["id"] as? String else { print("Invalid or missing identifier in user activity") return } if let jsonData = idString.data(using: .utf8) { do { let decoder = JSONDecoder() let persistentID = try decoder.decode(PersistentIdentifier.self, from: jsonData) openWindow(id: "Detail View", value: persistentID) } catch { print("Failed to decode PersistentIdentifier: \(error)") } } else { print("Failed to convert string to data") } }
Posted Last updated
.
Post marked as solved
3 Replies
129 Views
Is it possible to switch to a new View without using NavigationStack or NavigationLink or NavigationView? I know I can do it with a Bool, which either shows the second view, or the first, and then toggles the Bool. But can't I do something like this? Which obviously doesn't work. struct BasicButton: View { var buttonLabel = "Create User" var body: some View { Button { CreateUser() //another SwiftUI view, not a function } label: { Text(buttonLabel) } } }
Posted
by SergioDCQ.
Last updated
.
Post not yet marked as solved
2 Replies
119 Views
Is there a way to modify the SignInWithAppleButton so that only the Apple logo appears? According to Apple's guidelines, it's permissible to use only the icon. https://developer.apple.com/design/human-interface-guidelines/sign-in-with-apple Unfortunately, the button isn't customizable. However, I need a button that utilizes the logic of onRequest and onCompletion. Here's my current code using the standard SignInWithAppleButton. SignInWithAppleButton(.signIn, onRequest: { request in print("Apple ID Request") AppleSignInManager.shared.requestAppleAuthorization(request) }, onCompletion: { result in print("Apple ID Completion") handleAppleID(result) } ) .font(.title) .signInWithAppleButtonStyle(.white) .frame(height: 50) I tried exploring various SwiftUI customization options for the SignInWithAppleButton, such as adjusting its style or overlaying it with a custom image. I was expecting to find a way to remove the text and display only the Apple logo, as permitted by Apple's guidelines. However, I found that the SignInWithAppleButton isn't easily customizable in this way. So, I'm seeking guidance on alternative approaches to achieve the desired customization while still maintaining the functionality provided by the onRequest and onCompletion handlers.
Posted
by zikomiko.
Last updated
.