Use PHPicker in TabView's child view to return to the first Tab specified in the State in TabView instead of the original child view after using PHPicker.
When I close PHPicker, I use dismiss to close the screen as shown in the code below, and I am trying to figure out how to return to the original screen.
Please let me know if anyone knows.
ContentView:
struct ContentView:View {
enum Tabs:String {
case tab1 = "FirstView"
case tab2 = "SecondView"
case tab3 = "Account"
}
func returnNavigationTitle (tabSelection:Tabs) - > String {
switch tabSelection {
case.tab1:
return "FirstView"
case.tab2:
return "SecondView"
case.tab3:
return "Account"
}
}
@State private var navigationTitle: String=Tabs.tab1.rawValue
@StatevartabSelection:Tabs=.tab1
varbody:someView {
TabView (selection:$tabSelection) {
FirstView()
.tabItem{
Image(systemName: "house.fill")
Text (Tabs.tab1.rawValue)
}.tag(1)//.tabItem
SecondView()
.tabItem{
Image(systemName: "list.bullet")
Text (Tabs.tab2.rawValue)
}.tag(2)//.tabItem
PhotoPickerView()
.tabItem{
Image(systemName: "person.circle")
Text (Tabs.tab3.rawValue)
}.tag(3)//.tabItem
} .accentColor (.blue)
}
ImagePicker:
struct ImagePicker:UIViewControllerRepresentable{
@Environment(\.presentationMode) private var presentationMode
varsourceType:UIImagePickerController.SourceType=.photoLibrary
@Binding var selectedImage:UIImage
func makeUIViewController(context:UIViewControllerRepresentableContext<ImagePicker>)->UIImagePickerController{
let imagePicker=UIImagePickerController()
imagePicker.allowsEditing=true
imagePicker.sourceType=sourceType
imagePicker.delegate=context.coordinator
return imagePicker
}
US>func updateUIViewController(_uiViewController:UIImagePickerController, context:UIViewControllerRepresentableContext<ImagePicker>){
}
func makeCoordinator()->Coordinator {
Coordinator (self)
}
final class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
var parent —ImagePicker
init(_parent:ImagePicker){
self.parent = parent
}
funcimagePickerController(_picker:UIImagePickerController, didFinishPickingMediaWithInfo:[UIImagePickerController.InfoKey:Any]){
iflet image=info [UIImagePickerController.InfoKey.originalImage] as?UIImage{
parent.selectedImage=image
}
parent.presentationMode.wrappedValue.dismiss()
}
}
PhotoPickerView:
struct PhotoPickerView:View {
@State private var isShowingPhotoPicker=false
@State private var avatarImage=UIImage(named: "default-avatar")!
varbody:someView {
VStack {
Image(uiImage:avaterImage)
.resizable()
.scaledToFit()
.frame (width: 150, height: 150)
.clipShape (Circle())
.padding()
.onTapGesture{
isShowingPhotoPicker=true
}
Spacer()
}
.navigationTitle ("Profile")
.sheet(isPresented:$isShowingPhotoPicker, content:{
ImagePicker (sourceType:.photoLibrary, selectedImage:self.$avaterImage)
// PhotoPicker (avaterImage: $avaterImage)
})
}
Development Environment:
Xcode 13.3
Swift5
Invalid tag configuration.
TabView selection and .tag() must be the same.
.tag(Tabs.tab1)
© 2024 OneMinuteCode. All rights reserved.