4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[SwiftUI][iOS14]PageTabViewStyleでPageControlの色を変える

Posted at

iOS14からTabViewPageTabViewStyleが追加されました。iOS13では、UIPageViewControllerに相当するクラスがなかったのですが、PageTabViewStyleで実現できるようになったので、UIViewRepresentableを使わなくなるので便利です。
しかし、1つ難点がarimasu.UIPageControlが下の方に表示されるのですが、デフォルトで白でSwiftUIでは変更ができません。

色々調べた結果、UIKitのUIAppearanceを使うしか今のところ方法がないことがわかりました。

通常の使い方

UIPageControlの表示が白なので、背景を黒くしています。

struct ContentView: View {
    var body: some View {
        
        TabView {
            Text("タイトル1").foregroundColor(.white)
            Text("タイトル2").foregroundColor(.white)
        }.tabViewStyle(PageTabViewStyle.init(indexDisplayMode: .always))
        .background(Color.black)
        .edgesIgnoringSafeArea(.all)

    }
}

UIPageControlの色を変える

SwiftUIのみではUIPageControlのパーツの色を変えることはできませんが、UIAppearanceを使えば変更が可能です。

import SwiftUI
import UIKit

struct ContentView: View {
    
    init() {
        UIPageControl.appearance().currentPageIndicatorTintColor = .red
        UIPageControl.appearance().pageIndicatorTintColor = UIColor.white.withAlphaComponent(0.8)
    }
    
    var body: some View {
        
        TabView {
            Text("タイトル1").foregroundColor(.white)
            Text("タイトル2").foregroundColor(.white)
        }.tabViewStyle(PageTabViewStyle.init(indexDisplayMode: .always))
        .background(Color.black)
        .edgesIgnoringSafeArea(.all)

    }
}

参考

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?