0

I want make pagination with scrollview. it has another choice like TabView with rotationEffect but, it has a fundamental problem with ignoreSafeArea in my case. here's my code

import SwiftUI

struct GalleryView: View {
    var selected: MenuList.values?
    @State var activeTab: Int = 0
    @State var progress: Double = 0
    @State var imageTitle: String = ""
    @State var avgColor: UIColor = .clear
    
    init(tab: MenuList.values) {
        selected = tab
    }
    
    var body: some View {
        ZStack {
            Color(avgColor)
                .ignoresSafeArea()
            
            VStack(spacing: 0) {
                Text(imageTitle)
                    .foregroundColor(.secondary)
                
                ProgressBar()
                
                TabView(selection: $activeTab) {
                    if selected == .sky {
                        ForEach(Sky.allCases, id: \.title) { item in
                            TabImageView(item.title)
                                .tag(item.rawValue)
                        }
                    } else if selected == .night {
                        ForEach(Night.allCases, id: \.title) { item in
                            TabImageView(item.title)
                                .tag(item.rawValue)
                        }
                    }
                }
                .tabViewStyle(.page(indexDisplayMode: .never))
                .onChange(of: activeTab) { newValue in
                    if selected == .sky {
                        withAnimation(.easeInOut(duration: 0.3)) {
                            progress = Double(activeTab + 1) / Double(Sky.allCases.count)
                            imageTitle = Sky(rawValue: activeTab)?.title ?? ""
                            avgColor = UIImage(named: imageTitle)?.findAverageColor() ?? .clear
                        }
                    } else if selected == .night {
                        withAnimation(.easeInOut(duration: 0.3)) {
                            progress = Double(activeTab + 1) / Double(Night.allCases.count)
                            imageTitle = Night(rawValue: activeTab)?.title ?? ""
                            avgColor = UIImage(named: imageTitle)?.findAverageColor() ?? .clear
                        }
                    }
                }
                .onAppear() {
                    if selected == .sky {
                        imageTitle = Sky(rawValue: activeTab)?.title ?? ""
                        withAnimation(.easeInOut(duration: 0.3)) {
                            progress = Double(activeTab + 1) / Double(Sky.allCases.count)
                            avgColor = UIImage(named: imageTitle)?.findAverageColor() ?? .clear
                        }
                    } else if selected == .night {
                        imageTitle = Night(rawValue: activeTab)?.title ?? ""
                        withAnimation (.easeInOut(duration: 0.3)) {
                            progress = Double(activeTab + 1) / Double(Night.allCases.count)
                            avgColor = UIImage(named: imageTitle)?.findAverageColor() ?? .clear
                        }
                    }
                }
            }
            .navigationTitle(selected?.title ?? "error")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                Menu {
                    Button {
                        
                    } label: {
                        Text("small")
                    }
                    
                    Button {
                        
                    } label: {
                        Text("medium")
                    }
                    
                    Button {
                        
                    } label: {
                        Text("large")
                    }

                } label: {
                    Image(systemName: "tray.and.arrow.down.fill")
                }

            }
        }
        .ignoresSafeArea(.container, edges: .bottom)
    }
}

struct MainView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

extension GalleryView {
    func TabImageView(_ item: String) -> some View {
        GeometryReader {
            let size = $0.size
            
            ScrollView(showsIndicators: false) {
                VStack(spacing: 0) {
                    ZStack {
                        Image(item)
                            .resizable()
                            .aspectRatio(contentMode: .fill)
                            .frame(width: size.width, height: size.height)
                        
                        VStack(spacing: 0) {
                            Image(systemName: "chevron.compact.down")
                                .padding()
                        }
                        .frame(width: size.width, height: size.height, alignment: .bottom)
                    }
                    
                    MetaView()
                        .frame(width: size.width, height: size.height)
                }
            }
            .onAppear {
                UIScrollView.appearance().isPagingEnabled = true
            }
        }
        .ignoresSafeArea(edges: .bottom)
    }
    
    func ProgressBar() -> some View {
        ProgressView(value: progress)
            .progressViewStyle(.linear)
    }
}

it works fine on first tab. but on second tab or more, 'UIScrollView.appearance().isPagingEnabled = true' is no more affect. how can i solve this?

Martin Q
  • 7
  • 2

0 Answers0