UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

List searchable animation issue. Is SwiftUI really this buggy?

Forums > SwiftUI

Hi,

I have posted this question in a few places and it's not really been solved, so I thought I would ask here too to get your opinions.

I have just started learning SwiftUI and have created a basic View as follows:

import SwiftUI

struct ContentView: View {
    @State var searchText = ""
    @State var items = 0...5

    var body: some View {
        NavigationStack {
            List {
                ForEach(items, id: \.self) { item in
                    Text("Item \(item)")
                }
            }
            .searchable(text: $searchText)
            .navigationTitle("Test items")
        }
    }
}

When I tap the search field and the keyboard appears, the List jumps down the screen briefly, before animating. This happens consistently when running the app on my device (iPhone 11 Pro - iOS 16.3).

You can see the issue in the video here: https://reddit.com/link/10tql80/video/8mjpo7c0g8ga1/player

I have also tried running the app on the an iPhone 14 Simulator (iOS 16.2), where the problem always happens the first time you focus the search bar, then very infrequently after that.

I am using Xcode 14.2, but have also tried the same code with Xcode 14.0.

I would be grateful if someone could let me know if I'm doing something wrong here, or if SwiftUI is just incredibly buggy?

Thank you for any help in advance.

2      

@twoStraws offers a large number of useful videos along with great "How To" articles.

Did you watch his Instruments series?

See -> How to find buggy animations

One of Instrument's options is to document Animation Hitches.

Give it a go!

This is a forum for sharing education. Please run some Instrument profiles and come back and tell us what you learned!

2      

Here's a great article from Apple's Developer site on reporting bugs.

See -> Great Bug Reports

2      

I understand needing Instruments to diagnose issues with my own animations etc, but needing to debug the built-in animations for such a basic example would suggest to me that I'm doing something fundamentally wrong which I imagine would be obvious to someone with more experience than me, or SwiftUI is just very buggy.

If we take one of the examples written by @twoStraws here: https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-a-search-bar-to-filter-your-data

I have modifiered it slightly to show the border of each View for debug purposes:

import SwiftUI

extension View {
    func debugModifier<T: View>(_ modifier: (Self) -> T) -> some View {
        #if DEBUG
        return modifier(self)
        #else
        return self
        #endif
    }
}

extension View {
    func debugBorder(_ color: Color = .red, width: CGFloat = 1) -> some View {
        debugModifier {
            $0.border(color, width: width)
        }
    }
}

struct ContentView: View {
    let names = ["Holly", "Josh", "Rhonda", "Ted"]
    @State private var searchText = ""

    var body: some View {
        NavigationStack {
            List {
                ForEach(searchResults, id: \.self) { name in
                    NavigationLink {
                        Text(name)
                    } label: {
                        Text(name)
                            .debugBorder()
                    }
                }
            }
            .navigationTitle("Contacts")
            .debugBorder()
        }
        .searchable(text: $searchText)
        .debugBorder()
    }

    var searchResults: [String] {
        if searchText.isEmpty {
            return names
        } else {
            return names.filter { $0.contains(searchText) }
        }
    }
}

You can see the problem again from the iOS Simulator in the following GIF. The animation issue happens the first time you focus the Search field, but not the second time. On my physical iOS device the problem occurs every time though, which does suggest the performance of the device may be relevant.

GIF Here

It's hard to believe that the basic built-in components would be this buggy, so it leads me to believe that there could be some settings I'm missing to improve the app's performance. I have tried switching to the Release profile in Xcode but this doesn't improve things.

Alternatively this is just the state of SwiftUI and I need to accept that it's far from perfect, or use UIKit?

2      

@Benji  

Hi @GlassFisherman2279,

This won't help you much, but from my experience, a SwiftUI app can come with certain glitches which either depend on the version of iOS you are using or the version of Xcode they got build with.

You can test the first case by downloading a simulator for another version of iOS. As you already tried your app with 16.3 on your device and 16.2 in the simulator, you may want to try one of the 15.x branches (though they may be limited in their capabilities).

I can only talk for SwiftUI as that's my learning path. I have (almost) no experience with UIKit so I can't compare these two.

2      

Do you a Developer Account. If so use the Code-level support, they may find a bug and be able to help. Or file a Feedback

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.