My project about a suggestion field in swiftUI

Overview

SuggestionTextFieldMenu-SwiftUI

My project about a suggestion field in swiftUI

This is how it looks like!

ezgif com-video-to-gif-2

You can fill an array of objects and decide how many lines you want to show. I decided to show just a max of 3 lines and reduce them one by one through the filter.

So let's start from the beginning!

First of all, I created a struct that will contain my universal component. Declare the variables that you need, in this case, I declared these:

@Binding var editing: Bool

@Binding var inputText: String

@State var verticalOffset: CGFloat

@State var names: [String] = []
 
@State var horizontalOffset: CGFloat

after i decided to filter my array in that way:

private var filteredTexts: Binding<[String]> { Binding (
    get: {
        return names.filter { $0.contains(inputText) && $0.prefix(1) == inputText.prefix(1) } },
    set: { _ in })
}

I added an initializer with all that I need:

 public init(editing: Binding<Bool>, text: Binding<String>, verticalOffset: CGFloat, horizontalOffset: CGFloat) {
    self._editing = editing
    self._inputText = text
    self.verticalOffset = verticalOffset
    self.horizontalOffset = horizontalOffset
}

And here the core part, inside the body Add in this order: ScrollView, LazyVStack, and a ForEach to handle the showing of the view here's the code:

 ScrollView {
            LazyVStack(spacing: 0) {
                ForEach(filteredTexts.wrappedValue, id: \.self) { textSearched in
                    Text(textSearched)
                        .contentShape(Rectangle())
                        .onTapGesture(perform: {
                            inputText = textSearched
                            editing = false
                            self.endTextEditing()
                        })
                    Divider()
                        .padding(.horizontal, 10)
                }
            }
        }.frame(maxWidth: .infinity,
           minHeight: 0,
           maxHeight: 50 * CGFloat( (filteredTexts.wrappedValue.count > 3 ? 3: filteredTexts.wrappedValue.count)))

Finally, to use this just add a ZStack and inside put SuggestionTextFieldMenu, based on the position, you can show it above or under a text field. This method is not perfect, I tried to add a text field directly inside the universal component but when the small view comes up the entire view move. Please if you know how to reach that goal feel free to text me! Cheers

You might also like...
Sample iOS project built by SwiftUI + Flux and Combine framework using GitHub API
Sample iOS project built by SwiftUI + Flux and Combine framework using GitHub API

SwiftUI-Flux Flux enables us to have unidirectional data flow and make it testable. It's used to be implemented using RxSwift or ReactiveSwift in the

This is my first SwiftUI project, as I decided not to release it I made the codebase available here for anyone to take a look at.

Sunshine This is my first SwiftUI project, as I decided not to release it to the App Store. I made the codebase available here for anyone to take a lo

Sample iOS project built by SwiftUI + MVVM and Combine framework using GitHub API
Sample iOS project built by SwiftUI + MVVM and Combine framework using GitHub API

SwiftUI-MVVM One of the biggest idea for having MVVM is that most of data flow can be testable. Data binding in view layer by SwiftUI is awesome. Howe

Completed Project for Authentication in SwiftUI using Firebase Auth SDK & Sign in with Apple
Completed Project for Authentication in SwiftUI using Firebase Auth SDK & Sign in with Apple

Completed Project for Authentication in SwiftUI using Firebase Auth SDK & Sign in with Apple Follow the tutorial at alfianlosari.com Features Uses Fir

An iOS template project using SwiftUI, Combine and MVVM-C software architecture
An iOS template project using SwiftUI, Combine and MVVM-C software architecture

SwiftUI-MVVM-C A template project that uses SwiftUI for UI, Combine for event handling, MVVM-C for software architecture. I have done some small proje

The SwiftUI learning project.
The SwiftUI learning project.

The SwiftUI learning project.

Viagens (SwiftUi)  Project developed to study Swift
Viagens (SwiftUi) Project developed to study Swift

Viagens (SwiftUi) Project developed to study Swift Made with 💖 by Igor Sthaynny 📌 Contents 📌 Contents 📷 Screenshots 🚀 Technologies 💻 How to run

Pro Counter, SwiftUI WatchOS, Open Source Project
Pro Counter, SwiftUI WatchOS, Open Source Project

Countio | SwiftUI WatchOS App App Screenshots || What is Countio Countio is simple WatchOS App made with Swiftly SwiftUI. You can count anything quick

The demo project to show how to organize code to make SwiftUI apps easy to be test.
The demo project to show how to organize code to make SwiftUI apps easy to be test.

TestableApp I combined the idea to use functional programming instead of an loader instance in ModelView(I prefer to think of it as a service) and Res

Owner
Gabriele Cusimano
Hi! I'm an iOS Developer. I participated to the Apple Developer Academy in Naples and now I'm working for a company as iOS Developer!
Gabriele Cusimano
NewsAPI-Project - News API Project For iOS

NewsAPI-Project Es necesario descargar y realizar un pod install para ejecutar e

Álvaro Ferrández 0 Feb 3, 2022
A multiplatform SwiftUI project demonstrating various SwiftUI features.

WikiDemo A multiplatform SwiftUI project demonstrating various SwiftUI features, including creating a master-detail interface. It's a multiplatform ve

Swift Dev Journal 6 Oct 17, 2022
SwiftUI-MSALSample - Sample project to login with MSAL using SwiftUI

SwiftUI-MSALSample I could not find a good walkthrough on how to implement MSAL

Rob Evans 10 Nov 7, 2022
Matthew Asaminew 0 Jan 25, 2022
This SwiftUI project is a result of the third 'milestone' in "Hacking With Swift's 100 Days of SwiftUI".

exercise-tracker This SwiftUI project is a result of the third 'milestone' in "Hacking With Swift's 100 Days of SwiftUI". In this exercise tracking ap

Eric Tolson 2 Dec 14, 2022
Jetpack Compose and SwiftUI based Kotlin Multiplatform project

BikeShare Jetpack Compose and SwiftUI based Kotlin Multiplatform sample project (based on CityBikes API). Running on iOS (SwiftUI) macOS (SwiftUI) And

John O'Reilly 464 Jan 1, 2023
Currency Converter project coded by SwiftUI and Swift5

SwiftUI-Currency-Converter Currency Converter project coded by SwiftUI and Swift5 Features Implemented with SwiftUI Supports darkmode for sure SwiftUI

Alex.Liu 60 Oct 26, 2022
Building Expense Tracker iOS App with Core Data & SwiftUI Completed Project

Completed Project for Building Expense Tracker iOS App with Core Data & SwiftUI Follow the tutorial at alfianlosari.com Features Create, edit, and del

Alfian Losari 226 Dec 22, 2022
This is an example project of SwiftUI and Combine using GitHub API.

SwiftUI-Combine-Example This is an example project of SwiftUI and Combine using GitHub GET /search/users API. ?? Requirements Swift5.1 Beta Xcode11.0

Ryo Aoyama 436 Jan 5, 2023
Completed Project for Cross Platform Image Filter SwiftUI macOS & iOS App

Completed Project for Cross Platform Image Filter SwiftUI macOS & iOS App Follow the tutorial at alfianlosari.com Features Filter image using predefin

Alfian Losari 73 Dec 19, 2022