fermoya/SwiftUIPager

Precondition failure: invalid value type for attribute

rakesh1504 opened this issue · 3 comments

I am getting crash when I try to view Image in pager.

xcode 11.5
Simulator iPhone 11 Pro
iOS 13.5

Filename: SizeViewModifier.swift
line 23
Screenshot 2020-06-11 at 12 57 30 AM

My code where I am using it.
`
import CoreData
import SwiftUI
import SwiftUIPager

struct BoardPhotoView: View {
@Environment(\.presentationMode) var presentationMode
@FetchRequest(entity: Pedalboard.entity(), sortDescriptors: [
    NSSortDescriptor(keyPath: \Pedalboard.name, ascending: true),
    NSSortDescriptor(keyPath: \Pedalboard.imageD, ascending: true)
    ]
) var pedalboards: FetchedResults<Pedalboard>

@State var image : Data = .init(count: 0)

let pedalboard: Pedalboard
let imageArray:[Data]
// Magnification vars
@State private var scale: CGFloat = 1
@State var pageIndex = 0
// drag vars
@State private var position = CGSize.zero
@GestureState private var dragOffset = CGSize.zero

var body: some View {
            
    return
        ZStack {
            Color.newPrimaryColor2
            GeometryReader { proxy in
                VStack(spacing: 10) {
                    Pager(page: self.$pageIndex,
                          data: [0],
                          id: \.self) { page in
                            self.pageView(self.imageArray[page], index:page)
                    }
                    .disableDragging()
                    .itemSpacing(10)
                    //.padding(20)
                    .onPageChanged({ page in
                        withAnimation {
                            self.scale = 1.0
                            self.pageIndex = page
                        }
                    })
                    .frame(width: proxy.size.width,
                           height: proxy.size.height)
                    .background(Color.newPrimaryColor2.opacity(0.3))
                    
                    
                    Spacer()
                    
                  /*  HStack {
                        Spacer()
                        Button(action: {
                            withAnimation {
                                self.scale = 1.0
                                self.pageIndex = max(0, self.pageIndex - 1)
                            }
                        }, label: {
                            HStack(spacing: 10) {
                                Image(systemName: "backward.fill")
                                    .padding()
                                Text("Previous")
                            }
                        }).disabled(self.pageIndex <= 0)
                        Spacer()
                        Button(action: {
                            withAnimation {
                                self.scale = 1.0
                                self.position = .zero
                                self.pageIndex = min(self.imageArray.count - 1, self.pageIndex + 1)
                            }
                        }, label: {
                            HStack(spacing: 10) {
                                Text("Next")
                                Image(systemName: "forward.fill")
                                    .padding()
                            }
                        }).disabled(self.pageIndex >= self.imageArray.count - 1)
                        Spacer()
                    }*/
                    Spacer()
                }
            }
            
    }
}

func pageView(_ imagePic: Data, index: Int) -> some View {
    GeometryReader {reader in
        //Image("pizza1delivery")
        Image(uiImage: UIImage(data: imagePic)!)
        .resizable()
        .scaledToFit()
        .cornerRadius(5)
        .scaleEffect(index == self.pageIndex ? self.scale : 1)
        .shadow(radius: 5)
           
        .animation(.default)
        .offset(index == self.pageIndex ? CGSize(width: self.position.width + self.dragOffset.width, height: self.position.height + self.dragOffset.height) : .zero)
        .gesture(index != self.pageIndex ? nil : self.scale <= 1 ? nil : DragGesture()
            .updating(self.$dragOffset, body: { (value, state, transaction) in
            state = value.translation
        })
        .onEnded({ (value) in
            self.position.height += value.translation.height
            self.position.width += value.translation.width
        })
        )
            
        .gesture(index != self.pageIndex ? nil :MagnificationGesture()
            .onChanged({ (value) in
                print(index)
                self.scale = value.magnitude
            })
            .onEnded({ (value) in
                self.position = .zero
                self.scale = value.magnitude
            })
        )
    }
}
} 
struct BoardPhotoView_Previews: PreviewProvider {
static let moc = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
static var previews: some View {
    let pedalboard = Pedalboard(context: moc)
    // pedalboard.imageD = "sampleBoard"
      return NavigationView {
        BoardPhotoView(pedalboard: pedalboard, imageArray: [pedalboard.imageD!])
    }
}}

`

EDIT: It is working fine in Xcode 11.3.1

Hi @rakesh1504 ,

This seems to be an issue related to GeometryReader. If it works fine in Xcode 11.3.1 but not in a more recent versions of Xcode, then there's not much I can do.

I've found this is an issue reported to Apple, see here. The error doesn't say much, sorry I can't help you more.

I can just say that depending on how you have your views arranged, you might run into this error or not. Meaning that if for some reason, you wrap the whole body or part of it into a VStack it might magically work. I've run myself into this issue, seemed to be caused by a NavigationView and could fix it by using .navigationViewStyle(StackNavigationViewStyle()), see here.

My advice is, take your code and start removing parts of it and see when you don't see that error. When you've identified the exact part that makes it crash, try to change something (maybe simply add another modifier) to make it work. There are plenty of potential "solutions" in SO.

Some more useful links:

HI @rakesh1504, I'm testing this on the new Xcode 12 beta and this issue has been fixed in SwiftUI 2.0. In my case, I can reproduce the crash when building with Xcode 11.5 but not when building with Xcode 12 beta

Thank you @fermoya for the info. I will check.