Very weird bug where sheet cover is not working in Swift iOS 14.1-ios 14.4 but working for iOS 14.5 and above

Viewed 275

this bug has been scratching my head for the past few days and I still don't know why the problem is arising and what the fix is. I have a camera screen and integrated it with the TOCropViewController (https://github.com/TimOliver/TOCropViewController) to allow a user to select a picture from their photo library and crop it to show a new post. For some reason the image picker is detecting that it should change the view to the ImagePicker from the camera view screen but it's not displaying it on ios14.4 and below but it works just fine for iOS 14.5 and above.

Here is my camera view code:


struct CameraView: View {
    @StateObject var model = CameraModel()
    
    @State var currentZoomFactor: CGFloat = 1.0
    
    @StateObject var registerData = RegisterViewModel()

    @StateObject var newPostData = NewPostModel()
    
    enum SheetType {
        case imagePick
        case imageCrop
        case share
    }
    
    @State private var currentSheet: SheetType = .imagePick
    @State private var actionSheetIsPresented = false
    @State private var sheetIsPresented = false
    
    @State private var originalImage: UIImage?
    @State private var image: UIImage?
    @State private var croppingStyle = CropViewCroppingStyle.default
    @State private var croppedRect = CGRect.zero
    @State private var croppedAngle = 0

    @StateObject var userData = UserViewModel()


    var captureButton: some View {
        Button(action: {
            let impactMed = UIImpactFeedbackGenerator(style: .light)
            impactMed.impactOccurred()
            model.capturePhoto()
        }, label: {
            Circle()
                .foregroundColor(.white)
                .frame(width: 80, height: 80, alignment: .center)
                .overlay(
                    Circle()
                        .stroke(Color.black.opacity(0.8), lineWidth: 2)
                        .frame(width: 65, height: 65, alignment: .center)
                )
        })
    }
    
    var capturedPhotoThumbnail: some View {

        Group {
            RoundedRectangle(cornerRadius: 10)
                .frame(width: 55, height: 55, alignment: .center)
                .foregroundColor(Color.gray.opacity(0.2))
                .onTapGesture(perform: {
                   // newPostData.picker.toggle()
                    self.croppingStyle = .default
                    self.currentSheet = .imagePick
                    self.sheetIsPresented = true
                    print("HERE11 and \(self.currentSheet) and \(self.sheetIsPresented)")

                })
                .overlay(
                    Image("gallery")
                        .renderingMode(.template)
                        .resizable()
                        .frame(width: 25, height: 25)
                        .foregroundColor(Color("white")))
                
                //CODE WITH BUG on ios 14.4 and below. I tried a regular sheet as well that works on another view in ios 14.4 but it doesn't work in the cameraview()
                .sheet(isPresented: $sheetIsPresented) {
                    if (self.currentSheet == .imagePick) {
                        
                        
                        ImagePickerView(croppingStyle: self.croppingStyle, sourceType: .photoLibrary, onCanceled: {
                            // on cancel
                        }) { (image) in
                            guard let image = image else {
                                return
                            }
                            
                            self.originalImage = image
                            DispatchQueue.main.async {
                                self.currentSheet = .imageCrop
                                self.sheetIsPresented = true
                            }
                        }
                        
                    } else if (self.currentSheet == .imageCrop) {
                        ZStack {
                            Color("imagecropcolor").edgesIgnoringSafeArea(.all)
                            ImageCropView(croppingStyle: self.croppingStyle, originalImage: self.originalImage!, onCanceled: {
                                // on cancel
                            }) { (image, cropRect, angle) in
                                // on success
                                self.image = image
                                model.resetPhoto()
                                newPostData.newPost.toggle()
                            }
                        }
                    }
                }
        }
       
      
    }
    
    var flipCameraButton: some View {
        Button(action: {
            let impactMed = UIImpactFeedbackGenerator(style: .light)
            impactMed.impactOccurred()
            model.flipCamera()
        }, label: {
            Circle()
                .foregroundColor(Color.gray.opacity(0.2))
                .frame(width: 45, height: 45, alignment: .center)
                .overlay(
                    Image(systemName: "camera.rotate.fill")
                        .foregroundColor(.white))
        })
    }
    
    var body: some View {
        GeometryReader { reader in
            ZStack {
                Color.black.edgesIgnoringSafeArea(.all)
                
                VStack {
                    HStack{
                        
                        Button(action: {
                            model.switchFlash()
                        }, label: {
                            Image(systemName: model.isFlashOn ? "bolt.fill" : "bolt.slash.fill")
                                .font(.system(size: 20, weight: .medium, design: .default))
                        })
                        .accentColor(model.isFlashOn ? .yellow : .white)
                        .padding(.leading, 30)
                        
                        Spacer()
                        
                        if model.photo != nil {
                            Text("taken photo").onAppear{
                                newPostData.newPost.toggle()
                            }
                        }
                        //                Image(uiImage: model.photo.image!)
                        //                    .resizable()
                        //                    .aspectRatio(contentMode: .fill)
                        //                    .frame(width: 60, height: 60)
                        //                    .clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous))
                        //                    .animation(.spring())
                        //
                    }
                  
                    
                    CameraPreview(session: model.session)
                        .gesture(
                            DragGesture().onChanged({ (val) in
                                //  Only accept vertical drag
                                if abs(val.translation.height) > abs(val.translation.width) {
                                    //  Get the percentage of vertical screen space covered by drag
                                    let percentage: CGFloat = -(val.translation.height / reader.size.height)
                                    //  Calculate new zoom factor
                                    let calc = currentZoomFactor + percentage
                                    //  Limit zoom factor to a maximum of 5x and a minimum of 1x
                                    let zoomFactor: CGFloat = min(max(calc, 1), 5)
                                    //  Store the newly calculated zoom factor
                                    currentZoomFactor = zoomFactor
                                    //  Sets the zoom factor to the capture device session
                                    model.zoom(with: zoomFactor)
                                }
                            })
                        )
                        .onAppear {
                            model.configure()
                        }
                        .alert(isPresented: $model.showAlertError, content: {
                            Alert(title: Text(model.alertError.title), message: Text(model.alertError.message), dismissButton: .default(Text(model.alertError.primaryButtonTitle), action: {
                                model.alertError.primaryAction?()
                            }))
                        })
                        .overlay(
                            Group {
                                if model.willCapturePhoto {
                                    Color.black
                                }
                            }
                        )
                        .animation(.easeInOut)
                    
                    
                    HStack {
                        capturedPhotoThumbnail
                        
                        Spacer()
                        
                        captureButton
                            .padding(.trailing, 10)
                        
                        Spacer()
                        
                        flipCameraButton
                        
                    }
                    .padding(.horizontal, 20)
                    .padding(.bottom, 20)

                    
                    
                }
            }.fullScreenCover(isPresented: $newPostData.newPost) {
                if model.photo == nil {
                    NewPost(imageData: (self.image?.pngData())! )
                    
                } else {
                    NewPost(imageData: model.photo.originalData)

                }
            }
        }
    }
}

Here is where the CameraView() gets called from my Home Screen

import SwiftUI
import Firebase

struct Home: View {
    @AppStorage("current_status") var status = false
    @AppStorage("showSheet") var showSheet = false

    @State var loadedPost = Post(id: 0, PostUID: "", PostName: "", selectedForPost: false, time: Date())

    @State var selectedTab = "camera"
    
    var edges = UIApplication.shared.windows.first?.safeAreaInsets
    
    @StateObject var modelData = ModelView()
    
    @StateObject var userData = UserViewModel()

    
    
    var body: some View {
        VStack(spacing: 15){
  
            VStack (spacing: 0) {
                
                GeometryReader{_ in
                    ZStack{
                        if selectedTab == "Post"{
                            Post(loadedPost: $loadedPost, selectedTab: $selectedTab)
                        }else if selectedTab == "camera"{
                            CameraView() 
                        }else if selectedTab == "user"{
                            User(selectedTab: $selectedTab, loadedPost: $loadedPost)
                        }
                    
                    }
                    
                }.onChange(of: selectedTab) { (_) in
                    switch(selectedTab){
                        case "Post": if
                            !modelData.isPostLoad{modelData.loadPost()}
                        case "camera": if
                            !modelData.isCameraLoad{modelData.loadCamera()}
                        case "user": if
                            !modelData.isUserLoad{modelData.loadUser()}
                        default: ()
                    }
                    
                }
                

                
                
                
                //Tabview hide to show friend modal
                
                if !showSheet{
                    Divider()

                    HStack(spacing: 0) {

                            Spacer(minLength: 0)
                            TabButton(title: "Post", selectedTab: $selectedTab)
                            Spacer(minLength: 0)
                            TabButton(title: "camera", selectedTab: $selectedTab)
                                .padding(.leading, 30)
                                .padding(.trailing, 30)

                            Spacer(minLength: 0)
                            TabButton(title: "user", selectedTab: $selectedTab)
                            Spacer(minLength: 0)




                        
                    }
                    .padding(.horizontal, 30)
                    .padding(.bottom, edges!.bottom == 0 ? 15 : edges!.bottom)
                    .background(Color.black)
                    
                }
               
                
            }
            .ignoresSafeArea(.all, edges: .bottom)
            .background(Color("Black").ignoresSafeArea(.all, edges: .all))
            
            
        }
    }
}
//Tab Button
struct TabButton : View {
    var title: String
    @Binding var selectedTab: String
    var body: some View {
        Button(action: {
            withAnimation{selectedTab = title}
        }) {
            VStack(spacing: 5) {
                
                //Top indicator
                
                //Custom shape...
                if title == "user" {
                    Image(title)
                        .renderingMode(.template)
                        .resizable()
                        .foregroundColor(selectedTab == title ? Color.white : Color("Grey"))
                        .frame(width: 26.5, height: 26.5)
                        .padding(.top, UIScreen.screenHeight < 500 ? -5 : 15)
                }else if title == "camera"{
                    Image(title)
                        .renderingMode(.template)
                        .resizable()
                        .foregroundColor(selectedTab == title ? Color.white : Color("Grey"))
                        .frame(width: 40, height: 40)
                        .padding(.top, UIScreen.screenHeight < 500 ? -5 : 15)
                }else{
                    Image(title)
                        .renderingMode(.template)
                        .resizable()
                        .foregroundColor(selectedTab == title ? Color.white : Color("Grey"))
                        .frame(width: 32.5, height: 32.5)
                        .padding(.top, UIScreen.screenHeight < 500 ? -5 : 15)
                }
                
                
                
            }
        }
    }
}


//can update with load views here
class ModelView: ObservableObject {

    @Published var isPostLoad = false
    @Published var isCameraLoad = false
    @Published var isUserLoad = false

    init() {
        //load initial data
        isCameraLoad = true
        print("Home Data Loaded")
    }
    
    func loadPost(){
        print("Post Loaded")
        isPostLoad = true
    }
    
    func loadCamera(){
        print("Camera Loaded")
        isCameraLoad = true
    }
    
    func loadUser(){
        print("User loaded")
        isUserLoad = true
    }
}

I would greatly appreciate any help on how to get the ImagePicker view to show up for iOS 14.1-ios 14.4 I've been scratching my head since I worked on it assuming anything that works on iOS 14.5 and above should work on below but only this specific ImagePicker is not working as intended. Thanks!

0 Answers
Related