如何更改SwiftUI TabView点指示器的位置?

yrdbyhpb  于 11个月前  发布在  Swift
关注(0)|答案(2)|浏览(121)

这是我的TabView,我想把dot indicator的位置改到右下角,怎么改?

TabView {
        ForEach (modelData.features, id: \.id) { landmark in
            landmark.image
                .resizable()
                .scaledToFill()
                .frame(height: 200)
                .clipped()
        }
    }
    .frame(height: 200)
    .tabViewStyle(PageTabViewStyle(indexDisplayMode: .always))
    .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
    .indexViewStyle(PageIndexViewStyle()

字符串


的数据

ehxuflar

ehxuflar1#

使TabView的框架大于200

TabView {
    ForEach (modelData.features, id: \.id) { landmark in
        landmark.image
            .resizable()
            .scaledToFill()
            .frame(height: 200)
            .clipped()
    }
}
.frame(height: 260)
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .always))
.indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
.indexViewStyle(PageIndexViewStyle()

字符串

ee7vknir

ee7vknir2#

我对此进行了一点斗争,但发现这比任何其他解决方案都好。使用这种方法,您可以通过擦洗保持原生行为&逐渐消失的点。沿着可以灵活地将其放置在任何地方。

struct CustomPageControl: UIViewRepresentable {
    
    let numberOfPages: Int
    @Binding var currentPage: Int
    
    func makeUIView(context: Context) -> UIPageControl {
        let view = UIPageControl()
        view.numberOfPages = numberOfPages
        view.backgroundStyle = .prominent
        view.addTarget(context.coordinator, action: #selector(Coordinator.pageChanged), for: .valueChanged)
        return view
    }
    
    func updateUIView(_ uiView: UIPageControl, context: Context) {
        uiView.numberOfPages = numberOfPages
        uiView.currentPage = currentPage
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    class Coordinator: NSObject {
        var parent: CustomPageControl
        
        init(_ parent: CustomPageControl) {
            self.parent = parent
        }
        
        @objc func pageChanged(sender: UIPageControl) {
            parent.currentPage = sender.currentPage
        }
    }
}

个字符

相关问题