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

SwiftUI Documentation

Post

Replies

Boosts

Views

Activity

MapKit mapItemDetailAccessory Custom View
Hi all, It wasn't extensively covered in the "Unlock the power of places with MapKit" at WWDC, but is it possible to add your own views to the mapItemDetailAccessory? The default view is great, but I would like to add a button for opening a new window showing another view. The documentation is rather limited at the moment so I thought I would ask here. Thanks in advance.
0
0
18
1h
How to embed SwiftData model container in a custom file package document type
Hi all, I have done a lot of research on this but am not able to come up with a workable solution. Background: I am trying to make an universal app on macOS/iOS that organizes media (image/video/pdf); think of its functionality like Apple's Photos app. So for my app document type, I would have my custom file package and within the package folder, there would be a SwiftData model container file and folders to hold user media. Approaches taken: DocumentGroup with SwiftData model then write directly into the file package In my App scene, I create document by having DocumentGroup(editing: .customDocument, migrationPlan: CustomMigrationPlan.self). This will create a document with a model container with my SwiftData model. And when I need to add media files into the document, I get the URL of the document, then use FileManager to write the file into the desired folder in the document. The result is that the media file is saved in the file package but then the SwiftData container is corrupted (all model data is reset to empty.) I am now trying to: 2. DocumentGroup with custom file package then try to embed SwiftData container In my current approach, I would create a document by having DocumentGroup(newDocument: CustomFileDocument()). I have custom FileDocument and FileWrapper. But the problem is I don't know how to embed a SwiftData container into my FileDocument. Is it possible to create a SwiftData model container when my FilerWrapper initialize? I can't figure out how to do this. Can anyone please advice on how I should accomplish this? Or if maybe I am looking at this problem wrongly? Do I need to use AppKit/UIKit with Core Data because it's currently not possible with SwiftUI/SwiftData? Thank you so much for reading and any input is greatly appreciated.
0
0
27
7h
SwiftUI onDrop not working with DropDelegate on iOS18
I am new to SwiftUI, and I wrote a ReorderableForEach struct with onDrag and onDrop, which is working well on iOS17. However it's not wokring on iOS18 beta1 or beta2 on my iPhone or simulator. When I long press the item, nothing happens. When I remove onDrop, I can perform a Drag animation, so I think something wrong with my onDrop code. the whole code: import UIKit import SwiftUI public typealias Reorderable = Identifiable & Equatable struct GridData: Identifiable, Equatable { let id: Int } public struct ReorderableForEach<Item: Reorderable, Content: View, Preview: View>: View { @Binding private var active: Item? @State private var hasChangedLocation = false private let items: [Item] private let content: (Item) -> Content private let preview: ((Item) -> Preview)? private let moveAction: (IndexSet, Int) -> Void private var onDropAction: ((Item) -> Void)? private var allowReorder: Bool public init( _ items: [Item], active: Binding<Item?>, allowReorder: Bool, @ViewBuilder content: @escaping (Item) -> Content, @ViewBuilder preview: @escaping (Item) -> Preview, moveAction: @escaping (IndexSet, Int) -> Void, onDropAction: ((Item) -> Void)? = nil ) { self.items = items self._active = active self.allowReorder = allowReorder self.content = content self.preview = preview self.moveAction = moveAction self.onDropAction = onDropAction } public init( _ items: [Item], active: Binding<Item?>, allowReorder: Bool, @ViewBuilder content: @escaping (Item) -> Content, moveAction: @escaping (IndexSet, Int) -> Void, onDropAction: ((Item) -> Void)? = nil ) where Preview == EmptyView { self.items = items self._active = active self.allowReorder = allowReorder self.content = content self.preview = nil self.moveAction = moveAction self.onDropAction = onDropAction } public var body: some View { ForEach(items) { item in if !allowReorder { contentView(for: item) } else if let preview { contentView(for: item) .onDrag { return dragData(for: item) } preview: { preview(item) } } else { contentView(for: item) .onDrag { return dragData(for: item) } } } } private func contentView(for item: Item) -> some View { content(item) .opacity(active == item && hasChangedLocation ? 0.5 : 1) .onDrop( of: [.text], delegate: ReorderableDragRelocateDelegate( item: item, items: items, active: $active, hasChangedLocation: $hasChangedLocation ) { from, to in withAnimation { moveAction(from, to) } } onDropAction: { item in onDropAction?(item) } ) } private func dragData(for item: Item) -> NSItemProvider { active = item return NSItemProvider(object: "\(item.id)" as NSString) } } struct ReorderableDragRelocateDelegate<Item: Reorderable>: DropDelegate { let item: Item var items: [Item] @Binding var active: Item? @Binding var hasChangedLocation: Bool var moveAction: (IndexSet, Int) -> Void var onDropAction: ((Item) -> Void)? func dropEntered(info: DropInfo) { guard item != active, let current = active else { return } guard let from = items.firstIndex(of: current) else { return } guard let to = items.firstIndex(of: item) else { return } hasChangedLocation = true if items[to] != current { moveAction(IndexSet(integer: from), to > from ? to + 1 : to) } } func dropUpdated(info: DropInfo) -> DropProposal? { DropProposal(operation: .move) } func performDrop(info: DropInfo) -> Bool { hasChangedLocation = false active = nil onDropAction?(item) return true } } struct ReorderableDropOutsideDelegate<Item: Reorderable>: DropDelegate { @Binding var active: Item? func dropUpdated(info: DropInfo) -> DropProposal? { DropProposal(operation: .move) } func performDrop(info: DropInfo) -> Bool { active = nil return true } } public extension View { func reorderableForEachContainer<Item: Reorderable>( active: Binding<Item?> ) -> some View { onDrop(of: [.text], delegate: ReorderableDropOutsideDelegate(active: active)) } } #Preview { ReorderableForEach( (1...10).map { GridData(id: $0) }, active: .constant(nil), allowReorder: true ) { item in Text("Item \(item.id)") .padding() .background(Color.blue) .cornerRadius(8) } preview: { item in Text("Preview \(item.id)") .padding() .background(Color.red) .cornerRadius(8) } moveAction: { from, to in print("Move from \(from) to \(to)") } } Am I missing something or this is a bug on iOS18? Thanks in advance.
0
0
30
9h
iOS18 beta2: NavigationStack, Views Being Popped Automatically
This is a report of an issue that appears to be a regression regarding NavigationStack. I have been aware of an issue where views are being automatically popped within NavigationView / NavigationStack since iOS 15, and it seems to be reoccurring in iOS 18.0 beta2. Below is the reproducible code. Additionally, in my environment, this problem does not occur iOS 18 simulator, but it does happen on an iPhone XS Max(real device) with iOS 18 beta 2. Environment: Xcode: Version 16.0 beta (16A5171c) iOS: 18.0 (22A5297f) iPhone: XS Max (real device) import SwiftUI @main struct iOS16_4NavigationSample2App: App { var body: some Scene { WindowGroup { NavigationStack { NavigationLink { ContentView() } label: { Text("Content") } } } } } enum Kind { case none, a, b, c } struct Value: Hashable, Identifiable { let id: UUID = UUID() var num: Int } @MainActor class ContentModel: ObservableObject { @Published var kind: Kind = .a @Published var vals: [Value] = { return (1...5).map { Value(num: $0) } }() init() {} } struct ContentView: View { @StateObject private var model = ContentModel() @State private var selectedData: Value? @State private var isShowingSubView = false @Environment(\.dismiss) private var dismiss init() { } var body: some View { if #available(iOS 16.0, *) { List(selection: $selectedData) { ForEach(model.vals) { val in NavigationLink(value: val) { Text("\(val.num)") } } } .navigationDestination(isPresented: .init(get: { selectedData != nil }, set: { newValue in if !newValue && selectedData != nil { selectedData = nil } }), destination: { SubView(kind: model.kind) }) } } } struct SubView: View { init(kind: Kind) { print("init(kind:)") } init() { print("init") } var body: some View { Text("Content") } } This code was shared in a different issue [iOS 16.4 NavigationStack Behavior Unstable].
2
0
44
9h
Is it possible to loop a ScrollView to repeat infinitely?
I have a horizontal scroll view and a fixed array. I would like to loop it such that when I scroll left and get near the end, the array will add the items in the beginning to the end so that the user can continue to scroll. I would like this to happen when scrolling both left and right and to not have the current position of the user jump around. Here is my code. What am I missing? Would appreciate any and all help. import SwiftUI import Algorithms struct ContentView: View { @State private var timePosition = ScrollPosition(idType: Int.self, edge: .leading) @State private var times: [Int] = Array(0...23) var body: some View { ScrollView(.horizontal, showsIndicators: false) { LazyHStack(spacing: 0) { ForEach(times, id:\.self) { time in Text("\(time)") .font(.system(.callout, design: .monospaced, weight: .semibold)) .padding(8) .frame(width: 180, height: 110, alignment: .topLeading) .border(width: 1, edges: [.leading, .trailing], color: .primary.opacity(0.05)) .id(time) } } .scrollTargetLayout() } .frame(height: 110) .scrollPosition($timeScrollPosition, anchor: .center) .onScrollTargetVisibilityChange(idType: Int.self) { timeIDs in if let currentViewID = model.timeScrollPosition.viewID { if timeIDs.contains(times[times.count - 2]) { times.rotate(toStartAt: times.count - 1) } if timeIDs.contains(times[1]) { times.rotate(toStartAt: times.count-1) } print("New times: \(times)") timeScrollPosition.scrollTo(id: currentViewID) } } } }
0
0
38
16h
Obscuration of Specific Content in Screenshots on visionOS
Hello, I am currently developing an app using visionOS, and I am looking for a way to obscure specific content in screenshots. My app includes certain content that is exclusive to premium users, and I need to hide these parts when screenshots are shared on social media. In iOS, I was able to achieve this by extending SecureField to hide specific Views or Images, but this method does not work in visionOS. I am seeking advice on the best approach to implement this feature in visionOS. https://github.com/yoxisem544/ScreenshotPreventing-iOS Specifically, I would appreciate guidance on the following points: The optimal method for obscuring specific Views or Images in screenshots on visionOS Any tips or tricks when using existing components similar to SecureField Techniques or approaches used by other developers to implement similar features Your assistance would be greatly appreciated. Thank you for your help.
0
0
41
20h
Pop to root view when selected tab is tapped again for WebKit
Hello everyone! I have a WKWebView in my swiftui app and would like to enable to "pop root view when selected tab is tapped again" feature, but I have been unable to figure out how to implement this. Here's the basic code: class TabIdentifierModel:ObservableObject { @Published var tabSelection:TabIdentifier { willSet { if newValue == tabSelection { NotificationCenter.default.post(name: .popRootView, object: nil, userInfo: ["tab": newValue.rawValue]) } } } init() { tabSelection = .home } } struct ContentView: View { @AppStorage(AppStorageKeys.enableShorts) var enableShorts = true @StateObject var storeVM = StoreVM() @StateObject var downloadURLManager = DownloadURLManager.shared @State var downloadViewIsOpen = false // ...... @State var tabSelection = TabIdentifierModel() var body: some View { TabView(selection: $tabSelection.tabSelection) { // ...... WebViewWrapper(url: $libraryTabURL) .tabItem { Label { Text("Library") } icon: { Image(systemName: "folder.fill") } }.tag(TabIdentifier.library) // ...... } .environmentObject(tabSelection) } } Tapping on the tab again doesn't seem to set the value again thus the NotificationCenter.default.post is not sent and the web view is not reloaded. Help would be much appreciated! Thanks in advance!
0
0
48
1d
State restoration in Document-based iOS app
Greetings - If I build an app using DocumentGroup, I get the document picker when the app launches. A good iOS app should restore the state the app was in when it was closed. It seems like with DocumentGroup, when the user leaves my app and the app is killed, the next time they launch it, they have to remember the document they were working on and reopen it. How can I have my DocumentGroup-based app restore the last open document at launch? FB13874563 Thanks - Steve
0
0
43
1d
quickLookPreview keyboard issues on macOS
I'm trying to add QuickLook previews to a SwiftUI app which uses Table(). I've added the .quickLookPreview() modifier to my Table(), and added a menu command for invoking it, and if I select that menu item manually, it works fine, but I have two keyboard related issues which are making it difficult to actually ship this functionality: When using the .quickLookPreview() variant for a set of URLs, keyboard navigation between the quicklook previews only works with left/right arrows, but being invoked by a Table, it would make much more sense for up/down arrows to navigate through the previews I set a .keyboardShortcut() on the menu command to use Space, since that's the normally-expected shortcut for quicklook, and it doesn't work. If I set it to some random other key (like "a") it does work, but .space doesn't do anything.
2
0
55
1d
The button click for real-time activity did not work.
We added a button in the real-time activity that responds within the app upon clicking. Now we're encountering an issue where, if you immediately click this button after swiping down to enter the notification center, it only opens the app without any scheme response. You have to wait for about 1 second after swiping down before the button can respond normally. Could you please tell us why this is happening?
0
0
39
1d
SwiftData Update Item View from Background Thread
I have a background thread that is updating a swift data model Item using a ModelActor. The background thread runs processing an Item and updates the Item's status field. I notice that if I have a view like struct ItemListView: View { @Query private var items: [Items] var body: some View { VStack { ForEach(items) { item in ItemDetailView(item) } } } } struct ItemDetailView: View { var item: Item var body: some View { // expected: item.status automatically updates when the background thread updates the `Item`'s `status`. Text(item.status) // actual: This text never changes } } Then background updates to the Item's status in SwiftData does not reflect in the ItemDetailView. However, if I inline ItemDetailView in ItemListView like this: struct ItemListView: View { @Query private var items: [Items] var body: some View { VStack { ForEach(items) { item in // Put the contents of ItemDetailView directly in ItemListView Text(item.status) // result: item.status correctly updates when the background thread updates the item. } } } } Then the item's status text updates in the UI as expected. I suspect ItemDetailView does not properly update the UI because it just takes an Item as an input. ItemDetailView would need additional understanding of SwiftData, such as a ModelContext. Is there a way I can use ItemDetailView to show the Item's status and have the UI show the status as updated in the background thread? In case details about my background thread helps solve the problem, my thread is invoked from another view's controller like @Observable class ItemCreateController { func queueProcessingTask() { Task { let itemActor = ItemActor(modelContainer: modelContainer) await itemActor.setItem(item) await itemActor.process() } } } @ModelActor actor ItemActor { var item: Item? func setItem(_ item: Item) { self.item = modelContext.model(for: item.id) as? Item } func process() async { // task that runs processing on the Item and updates the Item's status as it goes. }
1
1
109
2d
scrollPosition(id:anchor:) freeze UI when show/hide keyboard
Hello ! This will be my first blog post. I hope I will find the solution. So, I try to use the new iOS 17+ API for ScrollView. I have an array of messages that I want to show in scroll view. And I need to scroll to bottom when I tap to Send button. When I use scrollPosition(id:anchor:), it freeze the UI. The link to the screencast to demonstrate. I think the problem might be the keyboard when it shows and hides. But I can't understand what can I do. struct ChatView: View { @ObservedObject var viewModel: CoachViewModel @State private var scrollTo: Message.ID? var body: some View { ScrollView { LazyVStack { ForEach(viewModel.messages) { message in MessageView(viewModel: viewModel, message: message) } } .scrollTargetLayout() } .scrollPosition(id: $scrollTo) .background(.primaryBackground) .onChange(of: viewModel.scrollToBottom) { if $1, let lastMessage = viewModel.messages.last { withAnimation { scrollTo = lastMessage.id } viewModel.scrollToBottom = false } } } } struct Message: Identifiable, Hashable { let id: UUID let author: MessageAuthor let text: String } final class CoachViewModel: ObservableObject { @Published var messageTextInput = "" @Published var scrollToBottom = false @Published var messages: [Message] = [...] // ... more code ... func sendMessage() { messages.append( .init( id: .init(), author: .user, text: messageTextInput ) ) messageTextInput.removeAll() scrollToBottom = true } } struct CoachView: View { @StateObject private var viewModel = CoachViewModel() @State private var isSearching = false var body: some View { VStack(spacing: 0) { Group { CoachTitleView(viewModel: viewModel, isSearching: $isSearching) ChatView(viewModel: viewModel) } .onTapGesture { UIApplication.shared.endEditing() } if isSearching { MatchboxView(viewModel: viewModel) } else { InputTextFieldView(viewModel: viewModel) } } } }
0
0
64
2d
Long Hang Times When Accessing View With Posts
For some reason, every time I'm accessing a view populated with posts and fetching posts, my app has like a 5-8 second hang time. I don't know whats causing this issue, and I have been dealing with this for 3 days. `func fetchPosts(completion: @escaping (Result<[PostWithUser], Error>) -> Void) { if !cachedPosts.isEmpty { completion(.success(self.cachedPosts)) return } postsRef.order(by: "timestamp", descending: true).getDocuments { [weak self] snapshot, error in guard let self = self else { return } if let error = error { completion(.failure(error)) return } guard let documents = snapshot?.documents else { completion(.success([])) return } let group = DispatchGroup() var postsWithUsers: [PostWithUser] = [] DispatchQueue.global(qos: .userInitiated).async { for document in documents { group.enter() let data = document.data() guard let userId = data["userId"] as? String, let parentId = data["parentId"] as? String, let groupId = data["groupId"] as? String, let text = data["text"] as? String, let isPinned = data["isPinned"] as? Bool, let imageUrl = data["imageUrl"] as? String, let videoUrl = data["videoUrl"] as? String, let timestamp = data["timestamp"] as? Timestamp, let views = data["views"] as? Int else { group.leave() continue } let post = Post(id: document.documentID, userId: userId, parentId: parentId, groupId: groupId, text: text, imageUrl: imageUrl, videoUrl: videoUrl, timestamp: timestamp.dateValue(), isPinned: isPinned, likedBy: [], views: views) self.usersRef.document(userId).getDocument { userDocument, error in defer { group.leave() } if let userDocument = userDocument, let userData = userDocument.data() { let user = User( id: userId, username: userData["username"] as? String ?? "", bio: userData["bio"] as? String ?? "", profilePictureUrl: userData["profileImageUrl"] as? String ?? "", privateProfile: userData["privateProfile"] as? Bool ?? false, privateFollowerList: userData["privateFollowerList"] as? Bool ?? false, privateFollowingList: userData["privateFollowingList"] as? Bool ?? false, privateReplies: userData["privateReplies"] as? Bool ?? false, privateLikes: userData["privateLikes"] as? Bool ?? false ) let postWithUser = PostWithUser(post: post, user: user) DispatchQueue.main.async { postsWithUsers.append(postWithUser) } } else { print("Failed to fetch user data for userId: \(userId), error: \(String(describing: error))") } } } group.notify(queue: .main) { self.cachedPosts = postsWithUsers completion(.success(postsWithUsers)) } } } }` ` func fetchPosts() { PostService.shared.fetchPosts { result in switch result { case .success(let posts): DispatchQueue.main.async { self.posts = posts } case .failure(let error): print("Failed to fetch posts: \(error)") } } } func refreshPosts() { PostService.shared.refreshPosts { result in switch result { case .success(let posts): DispatchQueue.main.async { self.posts = posts } case .failure(let error): print("Failed to fetch posts: \(error)") } } }
1
0
86
2d
SwiftUI SideMenu Navigation Issue
I am currently refactoring my app's side menu to be more like Twitter's. I have the UI down in terms of how the side menu looks and appears, but the issue is navigating to a view from the side menu. The views that a user can go to from the side menu are a mix of SwiftUI views & UIKit View Controllers. As of right now, when a user navigates to a view from the side menu, it presents it modally as a sheet. I want it to have regular navigation, where the user goes to the view displayed in full screen and can tap on the back button to go back to the previous view. Here is the associated code: SideMenuView.swift SideMenuViewModel.swift How can I modify the navigation logic to be like Twitter's? I've been stuck for days trying to find a fix but it has been a struggle.
1
0
104
3d