SwiftUI @State var initialization issue

SwiftUI doesn’t allow you to change @State in the initializer but you can initialize it. Remove the default value and use _fullText to set @State directly instead of going through the property wrapper accessor. @State var fullText: String // No default value of “” init(letter: String) { _fullText = State(initialValue: list[letter]!) }

How can I pop to the Root view using SwiftUI?

Setting the view modifier isDetailLink to false on a NavigationLink is the key to getting pop-to-root to work. isDetailLink is true by default and is adaptive to the containing View. On iPad landscape for example, a Split view is separated and isDetailLink ensures the destination view will be shown on the right-hand side. Setting isDetailLink … Read more

How to hide keyboard when using SwiftUI?

You can force the first responder to resign by sending an action to the shared application: extension UIApplication { func endEditing() { sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) } } Now you can use this method to close the keyboard whenever you desire: struct ContentView : View { @State private var name: String = … Read more

How do I create a multiline TextField in SwiftUI?

iOS 16 – beta TextFields can be configured to expand vertically using the new axis parameter. Also it takes the lineLimit modifier to limit the lines in the given range: TextField(“Title”, text: $text, axis: .vertical) .lineLimit(5…10) But the lineLimit modifier now also supports more advanced behaviors, like reserving a minimum amount of space and expanding … Read more

Add bottom border line to UI TextField view in SwiftUI / Swift / Objective-C / Xamarin

I am creating custom textField to make it reusable component for SwiftUI SwiftUI struct CustomTextField: View { var placeHolder: String @Binding var value: String var lineColor: Color var width: CGFloat var body: some View { VStack { TextField(self.placeHolder, text: $value) .padding() .font(.title) Rectangle().frame(height: self.width) .padding(.horizontal, 20).foregroundColor(self.lineColor) } } } Usage: @Binding var userName: String @Binding … Read more

How to remove the default Navigation Bar space in SwiftUI NavigationView

For some reason, SwiftUI requires that you also set .navigationBarTitle for .navigationBarHidden to work properly. NavigationView { FileBrowserView(jsonFromCall: URLRetrieve(URLtoFetch: applicationDelegate.apiURL)) .navigationBarTitle(“”) .navigationBarHidden(true) } Update As @Peacemoon pointed out in the comments, the navigation bar remains hidden as you navigate deeper in the navigation stack, regardless of whether or not you set navigationBarHidden to false in … Read more

Activity indicator in SwiftUI

As of Xcode 12 beta (iOS 14), a new view called ProgressView is available to developers, and that can display both determinate and indeterminate progress. Its style defaults to CircularProgressViewStyle, which is exactly what we’re looking for. var body: some View { VStack { ProgressView() // and if you want to be explicit / future-proof… … Read more

Make a VStack fill the width of the screen in SwiftUI

Try using the .frame modifier with the following options: .frame( minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading ) struct ContentView: View { var body: some View { VStack(alignment: .leading) { Text(“Hello World”) .font(.title) Text(“Another”) .font(.body) Spacer() } .frame( minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading ) .background(Color.red) } } … Read more