Description:When an object in a list (created from a fetchrequest) is deleted from a context, and the context is saved, the list does not properly update.Error:Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value (Thrown on line 5 below)struct DetailView: View { @ObservedObject var event: Event var body: some View { Text((event.timestamp!, formatter: dateFormatter)) .navigationBarTitle(Text(Detail)) } }Steps to reproduce:1. Create a new Master Detail App project with SwiftUI and Core Data.2. In the ContentView, set the body to a TabView with the first tab being the prebuilt NavigationView, and add a second arbitrary tab.struct ContentView: View { @Environment(.managedObjectContext) var viewContext var body: some View { TabView { NavigationView { MasterView() .navigationBarTitle(Text(Master)) .navigationBarItems( leading: EditButton(), trailing: Button( action: { withAnimation { Event.create(in: self.viewContext) } } ) { Image(systemName: plus) } ) Text(De
Search results for
SwiftUI List performance
50,605 results found
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hi everyone, I want to perform the tutorial from video WWDC19 - 204. Here is an example of building the Rooms application. At 11 minutes, the author of the video, Jacob Xiao, shows how he performs the extract subview operation. But when I try to repeat this operation an error occurs - Cannot find 'room' in scope. My code and code of Jacob Xiao are the same. import SwiftUI struct ContentView: View { var rooms: [Room] = [] var body: some View { NavigationView { List(rooms) { room in ExtractedView() } .navigationBarTitle(Text(Rooms)) } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView(rooms: testData) } } struct ExtractedView: View { var body: some View { NavigationLink( destination: Text(room.name)) { // Cannot find 'room' in scope Image(room.thumbnailName) .cornerRadius(8) VStack(alignment: .leading) { Text(room.name) Text((room.capacity) people) .font(.subheadline) .foregroundColor(.secondary) } } } } I think the reason is that
the only way to achieve this is to use ScrollView, but for a large amount of image lists there are some performance issues that do not occur with the List component.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags:
I'm replacing Settings screen on my app with SwiftUI. My App's Settings screen have a some fields that have a Toggle Switch, Title, and Subtitle. but It is a hard to correctly reproduces UIListContentConfiguration.subtitleCell() layout in SwiftUI. I'm trying by this code: Toggle(isOn: $someFlag) { VStack(alignment: .leading) { Text(Title) Text(some descriptions about this toggle switch).font(.footnote) } } screenshots: but it has some of issues: wrong font size; subtitle of SwiftUI version is little a bit bigger than UIKit version wrong padding; SwiftUI version has less padding than UIKit version, which makes hard to read (only little a bit, but I want to stick to its small differences) It might be solvable by manually adds padding, specify font sizes etc, but I feel It is wrong way. It should be exists a beautiful way to reproduce it. Is there a correct way to reproduce beautiful UIListContentConfiguration.subtitleCell() layout in SwiftUI? Thanks!
Backstory - I wanted to create a SwiftUI List with a collapsible header. I came up a solution that I will show below in code In short, there's an empty section the size of the header in the List, based on which I get an offset while scrolling and can move the header accordingly And it has been working great, except the only problem so far is that if you have a screen with this component and there's another screen presented modally that causes modifications of the content of the underlying screen with a List, this solution produces a random offset value of 39.5333333333333 The value is exactly the same no matter the size of the header I put in there, and I can't see what is causing it. This ONLY happens when modification happens from a modal screen. The style of the List also doesn't matter, bug happens on all of them So far I found that it happens due to us using UIKit navigation controllers in our app. Shown below are two examples of code, one with UIKit navigatio
Hi all, I am trying to measure the performce of my video game in iOS platform. After launching the debugger and caputre GPU workload (with frame selected as scope), I can not do performance profiling. The error message is: Failed to enable shader profiler. (516). And if I export the GPU trace and reopen it, the Xcode can not find any compatible devices connected. However, the same device used for capturing is connected. Device informations are: Mac device: MacBook Air M2, 2022 Mac OS version: Sonoma 14.4 XCode version: 15.2 (15C500b) mobile device: iPhone 13 Pro Max iOS version: 15.0 GPU performance counter can be performed using the same mobile device in my colleague's Mac. So I think there might be something wrong with my Xcode.
If you are talking about removing items from SwiftUI lists, the modifier for Mac apps is .onDeleteCommand instead of .onDelete, which iOS apps use. For anyone to provide more help, you have to provide the code to delete the item from Core Data and the SwiftUI code for deleting items. The following article may help you: https://www.swiftdevjournal.com/removing-items-from-swiftui-lists-in-mac-apps/
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags:
When you create a list of things in SwiftUI (using ForEach, for example) the items in the list need to have a unique identity, so that SwiftUI can tell them apart. The standard way of doing this is to make the type (a recipe step in this case) conform to protocol Identifiable, and then make sure that different steps have different (but persistent) id values. It sounds like your recipe steps all have the same identity, so everything in your list displays the same value, and changing one changes them all. I think it'd be helpful for you to spend some more time learning about how SwiftUI uses data in lists. There are plenty of 3rd party tutorials out there, as well as in Apple's documentation: https://developer.apple.com/tutorials/swiftui.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags:
So the first part is to do with Binding. To move is extremely simple: modifier onMove on ForEach a move func as described here https://www.hackingwithswift.com/quick-start/swiftui/how-to-let-users-move-rows-in-a-list struct ContentView: View { @State private var users = [Paul, Taylor, Adele] var body: some View { NavigationView { List { ForEach(users, id: .self) { user in Text(user) } .onMove(perform: move) } .toolbar { EditButton() } } } func move(from source: IndexSet, to destination: Int) { users.move(fromOffsets: source, toOffset: destination) } } However, in simulator, moving may be a bit whimsical. Better click, pause, move.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags:
You can use the onOpenURL(perform:) view modifier. https://developer.apple.com/documentation/swiftui/outlinesubgroupchildren/onopenurl(perform:) var body: some Scene { WindowGroup { ContentView() .onOpenURL { (url) in tttttttt// Handle url here tttttt} tt} }
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags:
Returning from a sheet nither a click or a tap gesture triggers on a component in a list, until you perform a drag gesture. The best is to see it, so to reproduce the situation I have created a little test. copy and play import SwiftUI enum TriggerType: String, CaseIterable { case button, tapGesture } enum ABC: String, CaseIterable { case A,B,C } struct ContentView: View { @State private var myLetters = [ABC]() @State private var triggerType = TriggerType.button var body: some View { List { // TriggerType Picker(selection: $triggerType, label: Text(no)) { ForEach(TriggerType.allCases, id: .self) { type in Text(type.rawValue) } } .pickerStyle(SegmentedPickerStyle()) // select letter LetterPicker(triggerType: $triggerType ,selected: $myLetters) .disabled(myLetters.count = 3) // view all selected letters ForEach(myLetters, id: .self) { letter in MyButton(triggerType: $triggerType, label: letter.rawValue, symbol: minus.circle.fill, symbolColor: .red) { if let index = myLetters.
There is currently no modifier to do this. SwiftUI doesn't have much in the way of customising list row accessories (I have previously filed feedback for this). The easiest way to get around this would be to use a Button instead and manually perform the navigation; the APIs in iOS 16 make this much easier. For example: @State private var path = NavigationPath() NavigationStack(path: $path) { List(newsList) { article in Button { path.append(article) } label: { NewsCell(news: article) } } .navigationDestination(for: Article.self) { article in NewsLetterView(news: article) } } There are some other hacky workarounds that you can find doing an online search, so you can go with that as well until a new modifier is released (iOS 17?).
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags:
A large number of images can always be a performance challenge. First, you should dig into where the performance challenge is actually appearing and then make the best decision given that. To do this, I would trace your interaction using Instruments. (See Explore UI animation hitches and the render loop and its two companion talks about commits and render hitches and how to fix them) From there, you are likely to see one of a few things: Latencies due to image loading PNG is a compressed format that is decoded entirely in software. You may explore JPEG or HEIC which should decode faster. (See Make blazing fast lists and collection views for some information on how images work) But more then that, you may realize you are doing it too often. You may want to keep a cache of CGImage/UIImages that you already have setup to avoid redoing that decoding work. Issues around MapKit commits MapKit might be struggling with how many elements you have. Consider using Clustering which should help.
Topic:
Developer Tools & Services
SubTopic:
Xcode
Tags:
i would strongly caution against proceeding full-on with SwiftUI, it is just plain broken in a lot of places. The list of things that don't work in SwiftUI seem to grow the more you use it. For instance, SwiftUI does not offer a pull to refresh feature (a deal breaker for me). SwiftUI provides no way of customizing the return key type on a keyboard. Keyboard support in general is just lacking, there is no way to set a first responder and no way to resign a first responder. Using UIHostingController to bridge UIKit code to SwiftUI leads to other issues like Lists not behaving as they should (NavigationLinks will remain highlighted after you return from the Detail View). And the list goes on and on.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags:
A great start to debugging hangs and hitches in your SwiftUI View is by profiling your app using the SwiftUI template in Instruments. This will enable you to assess potential runtime performance optimizations and identify any view properties or view body computations that may be impacting your data flow. The SwiftUI template in Instruments shows detailed runtime performance characteristics of your app in the following tracks: View Body: The timing summary and intervals of each body computation per module and view type. View Properties: The current values, summary and updates of each dynamic property for a view and module. Core Animation Commits: The summary, profile, samples and intervals of each Core Animation transaction commit for the process. Time Profile: The profile and samples of running threads on all cores at regular intervals for all processes. For more information, see the following WWDC sessions: Analyze hangs with Instruments Demystify SwiftUI
Topic:
UI Frameworks
SubTopic:
SwiftUI