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 yourEnvironmentObject
I suspect you are passing this view model to multiple views).Parchment is not ideal as mentioned by @Asperi.
PageView
wraps aUIViewController
as the paging controller, and each page is nested inside anotherUIHostingController
! A better approach is to makePagingController
a wrappedUIPageViewController
for the Scene conforming toUIViewControllerRepresentable
, withUIViewController
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 (setuserInteractionEnabled
to false in UIKit or View modifier.disabled(true)
in SwiftUI). This is ifParentView
is overlaid on top ofChildView
.