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

SwiftUI Documentation

Posts under SwiftUI tag

2,333 Posts
Sort by:
Post not yet marked as solved
1 Replies
543 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
28 Views
I have a situation where tapping on a NavigationLink on an item from a SwiftData Query results in an infinite loop, causing the app the freeze. If you run the code, make sure to add at least 1 item, then tap on it to see the issue. Here is the code for a small sample app I made to illustrate the issue: import SwiftUI import SwiftData @main struct TestApp: App { var sharedModelContainer: ModelContainer = { let schema = Schema([ Item.self ]) let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false) do { let container = try ModelContainer(for: schema, configurations: [modelConfiguration]) return container } catch { fatalError("Could not create ModelContainer: \(error)") } }() var body: some Scene { WindowGroup { ContentView() } .modelContainer(sharedModelContainer) } } struct ContentView: View { var body: some View { NavigationStack { ListsView() } } } struct ListsView: View { @Environment(\.modelContext) private var modelContext @Query(filter: #Predicate<Item> { _ in true }) private var items: [Item] var body: some View { List(items) { item in NavigationLink { ItemDetail() } label: { VStack { Text("\(item.name) | \(item.date.formatted())") } } } Button("Add") { let newItem = Item(name: "Some item", date: .now) modelContext.insert(newItem) try? modelContext.save() } } } struct ItemDetail: View { private var model = ItemModel() var body: some View { VStack { Text("detail") } } } fileprivate var count = 0 class ItemModel { var value: Int init() { value = 99 print("\(count)") count += 1 } } @Model final class Item { let name: String let date: Date init(name: String, date: Date) { self.name = name self.date = date } } In the test app above, the code in the initializer of ItemModel will run indefinitely. There are a few things that will fix this issue: comment out the private var model = ItemModel() line in ItemDetail view replace the @Query with a set list of Items move the contents of the ListsView into the ContentView instead of referencing ListsView() inside the NavigationStack But I'm not sure why this infinite loop is happening with the initializer of ItemModel. It seems like a SwiftData and/or SwiftUI bug, because I don't see a reason why this would happen. Any ideas? Has anyone run into something similar?
Posted Last updated
.
Post not yet marked as solved
0 Replies
32 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
72 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
85 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
54 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
.
Post not yet marked as solved
0 Replies
42 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
0 Replies
33 Views
I have a data object that dynamically changes the UIImage assigned to one of its instance variables, but when showing this image in SwiftUI, it's always black and white. The following sample code shows the difference between the same image, but using first the native constructor Image(systemName:) and then Image(uiImage:). When using AppKit and Image(nsImage:) this issue doesn't happen. import SwiftUI import UIKit struct ContentView: View { @State var object = MyObject() var body: some View { Image(systemName: "exclamationmark.triangle.fill") .symbolRenderingMode(.palette) .foregroundStyle(.white, .yellow) Image(uiImage: object.image) } } class MyObject { var image = UIImage(systemName: "exclamationmark.triangle.fill")! .applyingSymbolConfiguration(.init(paletteColors: [.white, .systemYellow]))! } #Preview { ContentView() }
Posted
by Nickkk.
Last updated
.
Post not yet marked as solved
0 Replies
32 Views
I'm trying to add a SpriteKit scene to my view using SpriteView(scene: spriteScene). But it's doesn't work the .sks scene doesn't display here is the image. (and this is the code :) import SwiftUI import _SpriteKit_SwiftUI struct Gameplay: View { let spriteScene: MainGameScene var body: some View { ZStack { ----------------------- SpriteView(scene: spriteScene) ---------------------------- } } }
Posted
by lujainy.
Last updated
.
Post not yet marked as solved
1 Replies
56 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
2 Replies
92 Views
I am using @AppStorage in a model object (see code below that works as expected). class ModelObject { static let shared = ModelObject() @AppStorage("enhanced") var scriptPickers: Bool = true var defaultDependentValue: String { scriptPickers ? "Enhanced" : "NOT enhanced" } } struct ContentView: View { @AppStorage("enhanced") var scriptPickers: Bool = true var body: some View { VStack { Toggle(isOn: $scriptPickers, label: { Text("userDefault val") }) Text("value: \(ModelObject.shared.defaultDependentValue)") } } } Now I want to test my model object in a way that will allow me to use a mock instance of UserDefaults, but am having trouble with the syntax. I tried adding a userDefaults var, and referring to the var in the @AppStorage class ModelObject { static let shared = ModelObject() let userDefaults: UserDefaults init(userDefaults: UserDefaults = .standard) { self.userDefaults = userDefaults } @AppStorage("enhanced", store: userDefaults) var scriptPickers: Bool = true var defaultDependentValue: String { scriptPickers ? "Enhanced" : "NOT enhanced" } } However I can't find a way to avoid the syntax error this generates: Cannot use instance member 'userDefaults' within property initializer; property initializers run before 'self' is available Any guidance on how I might be able to: continue using @AppStorage be able to test my class in a way that doesn't force me to use UserDefaults.standard thanks, in advance, Mike
Posted Last updated
.
Post not yet marked as solved
8 Replies
2.4k Views
I added a keyboard toolbar inside ZStack. And it works right after appears as expected But after opening and closing another view over the .sheet modifier, The keyboard toolbar doesn't show anymore. ZStack { . .. TextField() .toolbar { ToolbarItemGroup(placement: .keyboard) { .... } } } .sheet(isPresent: $binding) { Some other view } Seems it happened after upgrading to iOS 16. Does anybody has the solution? Thanks
Posted
by Drigin.
Last updated
.
Post not yet marked as solved
0 Replies
47 Views
When I try to share a image of a component using the ImageRenderer, the type is not .png while sharing the image created using ShareLink (The type is .jpeg for some reasons...) My code looks like this: ShareLink( "Share", item: ( ImageRenderer( content: shareView.frame( width: 420, height: 520 ) ).uiImage?.pngData() )!, preview: SharePreview( "Share Preview", image: ( ImageRenderer( content: shareView.frame( width: 420, height: 520 ) ).uiImage?.pngData() )! ) ) Also the image shared is always in low resolution, if anyone knows what to do in this case let me know! Any helps will be appreciated!
Posted Last updated
.
Post not yet marked as solved
0 Replies
56 Views
I have a Canvas inside a ScrollView on a Mac. The Canvas's size is determined by a model (for the example below, I am simply drawing a grid of circles of a given radius). Everything appears to works fine. However, I am wondering if it is possible for the Canvas rendering code to know what portion of the Canvas is actually visible in the ScrollView? For example, if the Canvas is large but the visible portion is small, I would like to avoid drawing content that is not visible. Is this possible? Example of Canvas in a ScrollView I am using for testing: struct MyCanvas: View { @ObservedObject var model: MyModel var body: some View { ScrollView([.horizontal, .vertical]) { Canvas { context, size in // Placeholder rendering code for row in 0..<model.numOfRows { for col in 0..<model.numOfColumns { let left: CGFloat = CGFloat(col * model.radius * 2) let top: CGFloat = CGFloat(row * model.radius * 2) let size: CGFloat = CGFloat(model.radius * 2) let rect = CGRect(x: left, y: top, width: size, height: size) let path = Circle().path(in: rect) context.fill(path, with: .color(.red)) } } } .frame(width: CGFloat(model.numOfColumns * model.radius * 2), height: CGFloat(model.numOfRows * model.radius * 2)) } } }
Posted
by Todd2.
Last updated
.
Post not yet marked as solved
0 Replies
55 Views
I have a swiftui iPhone app running on the "iOS Apps on Mac" simulator. What I'm trying to do is get a notification when the window size changes, but nothing seems to work. I have tried .onReceive(NotificationCenter.default.publisher(for: UIContentSizeCategory.didChangeNotification)) { _ in updateStuff() } I also tried .onAppear() { updateStuff() } but neither seems to get called any suggestions ?
Posted Last updated
.
Post not yet marked as solved
5 Replies
522 Views
hi I have been using WKWebView embedded in a UIViewRepresentable for displaying inside a SwiftUI View hierarchy, but when I try the same code on 17,5 beta (simulator) the code fails. In fact, the code runs (no exceptions raised or anything) but the web view does not render. In the console logs I see: Warning: -[BETextInput attributedMarkedText] is unimplemented Error launching process, description 'The operation couldn’t be completed. (OSStatus error -10814.)', reason '' The code I am using to present the view is: struct MyWebView: UIViewRepresentable { let content: String func makeUIView(context: Context) -> WKWebView { // Javascript that disables pinch-to-zoom by inserting the HTML viewport meta tag into <head> let source: String = """ var meta = document.createElement('meta'); meta.name = 'viewport'; meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no'; var style = document.createElement('style'); style.type = 'text/css'; style.innerHTML = '*:focus{outline:none}body{margin:0;padding:0}'; var head = document.getElementsByTagName('head')[0]; head.appendChild(meta); head.appendChild(style); """ let script: WKUserScript = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true) let userContentController: WKUserContentController = WKUserContentController() let conf = WKWebViewConfiguration() conf.userContentController = userContentController userContentController.addUserScript(script) let webView = WKWebView(frame: CGRect.zero /*CGRect(x: 0, y: 0, width: 1000, height: 1000)*/, configuration: conf) webView.isOpaque = false webView.backgroundColor = UIColor.clear webView.scrollView.backgroundColor = UIColor.clear webView.scrollView.isScrollEnabled = false webView.scrollView.isMultipleTouchEnabled = false if #available(iOS 16.4, *) { webView.isInspectable = true } return webView } func updateUIView(_ webView: WKWebView, context: Context) { webView.loadHTMLString(content, baseURL: nil) } } This has been working for ages and ages (back to at least ios 15) - something changed. Maybe it is just a problem with the beta 17.5 release?
Posted Last updated
.
Post not yet marked as solved
1 Replies
261 Views
I'm using Text(targetDate, style:.relative) to show how a countdown of minutes until a given event. Unfortunately, this includes seconds, which takes unnecessary space and can get quite distracting when used on the dynamic island. There are many examples of apps that show estimates like "7 minutes" or "7 min" but how do they get the countdown to update? Are background updates reliable enough for this? Surely they don't send a push notification every minute to update the remaining time? Or is there some formatting option that I'm missing?
Posted
by mhalttu_.
Last updated
.
Post not yet marked as solved
0 Replies
77 Views
I want to change the display language, particularly for week and the year and date on MultiDatePicker. By adjusting the locale, the year and date can be changed. However, I'm unable to change the display for week . I've tried several methods, such as setting the calendar and adjusting the time zone, but none of them seem to work. Are there any good way to solve it?
Posted
by hcrane.
Last updated
.