LoginSignup
1
0

【知見メモ】端末の外観モードに合わせてキーボードの外観モードを変更できないケース

Last updated at Posted at 2024-03-01

本記事について

端末の外観モードに合わせてキーボードの外観モードを変更できないケースについて紹介します。

環境

  • iOS 17.0
  • Xcode 15.2

調査概要

端末の外観モードに合わせてキーボードの外観モードを変更したいと要求がありました。
しかし前提として、info.plistのAppearanceをLightにしなければいけませんでした。
その条件でキーボードの色だけ外観モードに対応できるのか調査しました。

調査結果

  • info.plistのAppearanceをLightにすると、端末の外観モードの設定関係なく、UITraitCollectionのuserInterfaceStyleが常に.lightが返ってくる
  • info.plistのAppearanceをAutomaticにすると、上記の問題は解決できるが、ダークモード対応していないアプリだと難しい。
final class SampleViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .lightGray

        // NOTE: UIの設定
        let textField = UITextField()
        textField.backgroundColor = .white
        textField.borderStyle = .roundedRect
        textField.placeholder = "入力内容"
        textField.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(textField)
        NSLayoutConstraint.activate([
            textField.widthAnchor.constraint(equalToConstant: 100),
            textField.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            textField.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])

        // NOTE: キーボードの外観を変えようとする
        // info.plistのAppearanceをLightだと、userInterfaceStyleは常に.light
        switch traitCollection.userInterfaceStyle {
        case .light:
            textField.keyboardAppearance = .light
        case .dark:
            textField.keyboardAppearance = .dark
        default: break
        }
    }
}
1
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
1
0