Post

Replies

Boosts

Views

Activity

Reply to How do I pass a binding to a focus state?
I was able to get this to work on an iOS15 simulator with the following code structure: struct Parent: View { @FocusState var focusedField: UUID? var body: some View { VStack { Child(focusedField: $focusedField) Sibling(focusedField: $focusedField) } } } struct Child: View { var focusedField: FocusState<UUID?>.Binding @State var someText: String = "" @State var someTextFieldUUID: UUID = UUID() var body: some View { VStack { TextField("Focusable field", text: $someText)             .focused(focusedField, equals: someTextFieldUUID) } } } I checked to make sure I could update focus to parent and sibling fields programmatically. Note: I think the new focus API doesn't yet work on Xcode previews.
Jun ’21
Reply to How to add more padding bellow a TextView when the keyboard is shown
This seems to work. Maybe it can be optimized more. You could also parameterize the view modifier for customizable padding. ScrollView(.vertical) { content } .keyboardAvoiding() import SwiftUI import Combine public extension Publishers {     static var keyboardHeight: AnyPublisher<CGFloat, Never> {         let willShow = NotificationCenter.default.publisher(for: UIApplication.keyboardWillShowNotification)             .map { $0.keyboardHeight }         let willHide = NotificationCenter.default.publisher(for: UIApplication.keyboardWillHideNotification)             .map { _ in CGFloat(0) }         return MergeMany(willShow, willHide)             .eraseToAnyPublisher()     } } public extension Notification {     var keyboardHeight: CGFloat {         return (userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect)?.height ?? 0     } } public struct KeyboardAvoiding: ViewModifier {     @State private var keyboardActiveAdjustment: CGFloat = 0     public func body(content: Content) -> some View {         content             .safeAreaInset(edge: .bottom, spacing: keyboardActiveAdjustment) {                 EmptyView().frame(height: 0)             }             .onReceive(Publishers.keyboardHeight) {                 self.keyboardActiveAdjustment = min($0, <<YOUR PADDING>>)             }     } } public extension View {     func keyboardAvoiding() -> some View {         modifier(KeyboardAvoiding())     } }
Dec ’22
Reply to SwiftUI NavigationLink freezing when tapped
I'm having a similar issue when using SwiftData. If I do an if let desiredObject = modelContext.model(for: desiredObjectId) as ? Type (or the registeredModel version) in a .navigationDestination(item: $desiredObjectId), the Swift Data object will be found and assigned but the view doesn't finish initializing. Even a simple Text("") won't load. Adding a print statement to a View's init shows it printing infinitely.
Jan ’24
Reply to SwiftUI NavigationLink destination
This just seems like a bad approach. You're running into difficulties because that's not how it's supposed to be done. Look up the localization videos and documentation if you want to localize strings and images. Use conditional viewbuilders if you want to swap views based on locale. If you're not doing this for localization, I'd assume you're trying to fetch additional functionality dynamically from a server--that's disallowed. Also, turning everything into JSON means you lose things like code completion and compilation checks.
Jun ’24