0
0

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 1 year has passed since last update.

iPadのTabBar(UITabBar)にカスタムフォントを適用できない時の対処法

Last updated at Posted at 2023-09-10

背景

SwiftUIでTabViewを実装したとき、フォントを変えたかったのでUITabBarAppearanceを使っていた。
iPhoneでは上手く適用できていたが、iPadだとフォントが適用されていなくて困っていた。

HogeView.swift
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でも横画面時は画像左になるんすかね?
image.png
human-interface-guidelines/tab-bars

対処

インライン対応を入れる。

HogeView.swift
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
]

所感

inlineLayoutAppearance

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] グローバルな外観設定

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?