如何更改 PageViewController 的背景颜色?

How can I change background color of PageViewController?

如何更改白色区域的背景颜色?

我使用了 UIViewControllerRepresentable 但我不知道如何更改 UIViewControllers 的颜色。

我想我需要在 makeUIViewController 函数中更改背景颜色?

我不太懂英语,希望我能解释一下我的问题。

入职视图:

    struct OnboardingView: View {
        
        @State var currentPageIndex = 0
        
        let timer = Timer.publish(every: 2, on: .main, in: .common).autoconnect()
        var subviews = [
            UIHostingController(rootView: SubView(imageString: "1")),
            UIHostingController(rootView: SubView(imageString: "1")),
            UIHostingController(rootView: SubView(imageString: "1"))
        ]
        var titles = ["Take some time out", "Conquer personal hindrances", "Create a peaceful mind"]
        var captions =  ["Take your time out and bring awareness into your everyday life", "Meditating helps you dealing with anxiety and other psychic problems", "Regular medidation sessions creates a peaceful inner mind"]
        
        var body: some View {
            VStack(alignment: .leading) {
                Group {
                    Text(titles[currentPageIndex])
                        .font(.title)
                    Text(captions[currentPageIndex])
                        .font(.subheadline)
                        .frame(width: 300, height: 50, alignment: .center)
                        .lineLimit(nil)
                }
                .padding()
                
                PageViewController(currentPageIndex: $currentPageIndex, viewControllers: subviews)
                    .frame(height: 600)
                    .background(Color.yellow)
             ...
          }
     }
}
       

PageViewController:

struct PageViewController: UIViewControllerRepresentable {
    
    @Binding var currentPageIndex: Int
    
    var viewControllers: [UIViewController]
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    func makeUIViewController(context: Context) -> UIPageViewController {
        let pageViewController = UIPageViewController(
            transitionStyle: .scroll,
            navigationOrientation: .horizontal)
        
        pageViewController.dataSource = context.coordinator
        pageViewController.delegate = context.coordinator
        
        
        return pageViewController
    }
    
    func updateUIViewController(_ pageViewController: UIPageViewController, context: Context) {
        pageViewController.setViewControllers(
            [viewControllers[currentPageIndex]], direction: .forward, animated: true)
    }
    
    class Coordinator: NSObject, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
        
        var parent: PageViewController

        init(_ pageViewController: PageViewController) {
            self.parent = pageViewController
        }
        
        func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
            
            guard let index = parent.viewControllers.firstIndex(of: viewController) else {
                 return nil
             }
            
            if index == 0 {
                return parent.viewControllers.last
            }
 
            return parent.viewControllers[index - 1]
            
        }
        
        func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
            
            guard let index = parent.viewControllers.firstIndex(of: viewController) else {
                return nil
            }
            
            if index + 1 == parent.viewControllers.count {
                return parent.viewControllers.first
            }
            
            return parent.viewControllers[index + 1]
        }
        
        func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
            if completed,
                let visibleViewController = pageViewController.viewControllers?.first,
                let index = parent.viewControllers.firstIndex(of: visibleViewController)
            {
                parent.currentPageIndex = index
            }
        }
    }
}
 

您需要设置子视图的backgroundColor

您可以在 OnboardingViewPageViewController 中完成:

func makeUIViewController(context: Context) -> UIPageViewController {
    ...
    // make the subviews transparent
    viewControllers.forEach {
        [=10=].view.backgroundColor = .clear
    }
    ...
}