I have ObservableObject for my parent view and child view:
import Parchment
class MainViewModel: ObservableObject {
@Published var inputText = String()
init() {}
}
struct ChildView: View {
@EnvironmentObject var viewModel: MainViewModel
var body: some View {
PageView(/**/) {
TextField("", $viewModel.inputText)
}
}
}
}
I tried to put inputText in child view's ObservedObject but got no changes.
When I'm entering one letter, keyboard closes. I think that SwiftUI starts redrawing of whole page, and because of this TextField becomes inactive.
How to fix this? Maybe somehow activate TextField by force, or start keyboard?
Your current code doesn't compile on my 2.4.0 version of Parchment
, and SwiftUI so I've tried to infer various details - the code works fine in native SwiftUI components. I would consider re-architecting the code though so that the @Published
property references a Model object - this is better SoC and allows to be shared better (given your EnvironmentObject
I suspect you are passing this view model to multiple views).
Parchment is not ideal as mentioned by @Asperi. PageView
wraps a UIViewController
as the paging controller, and each page is nested inside another UIHostingController
! A better approach is to make PagingController
a wrapped UIPageViewController
for the Scene conforming to UIViewControllerRepresentable
, with UIViewController
s. I would just implement this from scratch than use Parchment - it may be overkill, and perhaps some aspects could be designed better.
I may be missing something, but it's hard to help without further code in context. Also, from your example, setting PageView
in the parent view makes more sense and that your subviews in parent view controller should not capture touches (set userInteractionEnabled
to false in UIKit or View modifier .disabled(true)
in SwiftUI). This is if ParentView
is overlaid on top of ChildView
.