I think this might only not work in previews. I was able to get my code (with the same structure) working as expected in an iOS15 simulator. The preview in Xcode did nothing.
Post
Replies
Boosts
Views
Activity
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.
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())
}
}
I've been able to resolve some of my deletion issues by nullifying the relationships prior to deleting the model object from the context.
I was dealing with that since updating to Beta 6 too. Now I'm seeing dyld[654]: Symbol not found: _$s9SwiftData20DefaultModelExecutorC7contextAcA0D7ContextC_tcfC.
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.
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.
It'd be a lot easier to help you if you posted the relevant code than trying to analyze the situation by downloading and using your app.