LoginSignup
1

More than 5 years have passed since last update.

modalPresentationStyleにoverCurrentContext を指定してもステータスバーの色を変える方法

Last updated at Posted at 2018-06-19

よくわからない題名になりました。

 modalPresentationStyle = .overCurrentContext
 modalTransitionStyle = .crossDissolve

などを指定して、 元の画面を見せながら透過のUIViewController(viewのbackgroundColorのalphaを1未満に指定など)を表示させるケース はUIによってはあると思うのですが、上記を指定した状態でステータスバーの色を変えようと思っても変わらないのではまらないようにしましょうという話です。(20分くらいplistの設定とか色々見てしまった。。)

親のViewControllerのステータスバーが例えば UIStatusBarStylelightContent などを指定している際に、

    open func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Swift.Void)? = nil)

で表示したいUIViewControllerなどに

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .default
    }

を指定するだけではステータスバーの色は変わりません。

 UIApplication.shared.statusBarStyle = .lightContent

などを viewWillAppear でコールしても勿論変わりません。

.overCurrentContext を指定しているわけですから。

そういう時には、

modalPresentationCapturesStatusBarAppearance = true

を指定してあげれば解決します!
何気に長い間iOS開発をしてきてこのプロパティ初めて使いました・・・。

    // This controls whether this view controller takes over control of the status bar's appearance when presented non-full screen on another view controller. Defaults to NO.
    @available(iOS 7.0, *)
    open var modalPresentationCapturesStatusBarAppearance: Bool

久々の投稿でした。

約2年のiOSアプリ開発で実際に使ったライブラリとそこから得られた知見

これを書いてから2年ほど経ったので、また同じようなものをSwift版でがっつり書こうと思います!

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