背景
SwiftUIでTabViewを実装したとき、フォントを変えたかったのでUITabBarAppearanceを使っていた。
iPhoneでは上手く適用できていたが、iPadだとフォントが適用されていなくて困っていた。
let appearance = UITabBarAppearance()
// フォント指定
appearance.stackedLayoutAppearance.normal.titleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: UIFont(name: "適当なフォント", size: 12) as Any
]
原因
UITabBarAppearance does not work on iOS15 iPad (title color)に書かれているように、iPadの場合はインライン(アイコンとテキストが隣り合う)表示であることでiPadの場合にフォントを適用できていないようだった。
→画像左のようにiPadはアイコンとテキストが隣り合う。iPhoneでも横画面時は画像左になるんすかね?
human-interface-guidelines/tab-bars
対処
インライン対応を入れる。
let appearance = UITabBarAppearance()
// iPhoneフォント指定
appearance.stackedLayoutAppearance.normal.titleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: UIFont(name: "適当なフォント", size: 12) as Any
]
// iPadフォント指定
appearance.inlineLayoutAppearance.normal.titleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: UIFont(name: "適当なフォント", size: 12) as Any
]
所感
The appearance attributes for items displayed with an inline style.
ってあるけどinline styleがどういうスタイルか良く分からんかったのがそもそもの敗因かなとか思った。
理解力ないのもあるけどinline styleについて画像とセットで書いてほしいとか思ったりもした。みんなこういうのどうやってググってんだろ。
あとhuman-interface-guidelines/tab-bars的にiPadはサイドバー使えっていうのはまぁハイ...そうすね。
参考
TabView
human-interface-guidelines/tab-bars
UITabBarAppearance
UITabBarAppearance does not work on iOS15 iPad (title color)
inlineLayoutAppearance
[iOS][SwiftUI] グローバルな外観設定