LoginSignup
2
1

More than 5 years have passed since last update.

Delphi で iOS のステータスバー色を変更するには

Last updated at Posted at 2017-03-28

iOS ステータスバーの色変更

たまに iOS のステータスバーの色を変更したいと思うときがあります。
ステータスバーはここの色のことです(オフィシャルより拝借)。

FireMonkeyStatusBariOS_Option1.png

Delphi 10.2 Tokyo の場合

TCommonCustomForm に SystemStatusBar というプロパティが増えて簡単に iOS のステータスバー色を変更できるようになりました!

詳しくはオフィシャルブログをご覧下さい。
Delphi / C++Builder iOSステータスバー色変更方法[JAPAN]

Delphi 10.1 Berlin 以前の場合

でも Delphi のバージョンが色々な事情があって Tokyo に上げられないなんて事もあると思います。

では、どうするか?というと、実は FireMonkey には TFMXViewController.SetStatusBarBackgroundColor というステータスバー色を変更するメソッドが存在します。

ですが、これは隠蔽されているため普通には呼び出せません。
そのため、RTTI を使って無理矢理呼び出します。

以下 RTTI を使って SetStatusBarBackgroundColor を呼び出すコードです。
めんどくさい!

procedure SetStatusBarColor(const iColor: TAlphaColor);
var
  RttiType: TRttiType;
  RttiField: TRttiField;
  RttiMethod: TRttiMethod;
  Cocoa: TObject;
  AppDelegate: TObject;
  FMXWindow: TObject;
  ViewControler: TObject;
  Intf: IInterface;
begin
  Intf := TPlatformServices.Current.GetPlatformService(IFMXApplicationService);
  if (Intf = nil) then
    Exit;

  Cocoa := (Intf as TObject);

  // TPlatformCocoaTouch
  RttiType := SharedContext.GetType(Cocoa.ClassType);
  if (RttiType = nil) then
    Exit;

  RttiField := RttiType.GetField('FAppDelegate');
  if (RttiField = nil) then
    Exit;

  AppDelegate := RttiField.GetValue(Cocoa).AsObject;
  if (AppDelegate = nil) then
    Exit;

  // TApplicationDelegate
  RttiType := SharedContext.GetType(AppDelegate.ClassType);
  if (RttiType = nil) then
    Exit;

  RttiField := RttiType.GetField('FMainWindow');
  if (RttiField = nil) then
    Exit;

  FMXWIndow := RttiField.GetValue(AppDelegate).AsObject;
  if (FMXWIndow = nil) then
    Exit;

  // TFMXWindow
  RttiType := SharedContext.GetType(FMXWIndow.ClassType);
  if (RttiType = nil) then
    Exit;

  RttiField := RttiType.GetField('RootViewController');
  if (RttiField = nil) then
    Exit;

  ViewControler := RttiField.GetValue(FMXWIndow).AsObject;
  if (ViewControler = nil) then
    Exit;

  // TFMXViewController
  RttiType := SharedContext.GetType(ViewControler.ClassType);
  if (RttiType = nil) then
    Exit;

  RttiMethod := RttiType.GetMethod('SetStatusBarBackgroundColor');
  if (RttiMethod = nil) then
    Exit;

  RttiMethod.Invoke(ViewControler, [iColor]);
end;

と、まあ、こんな感じで Tokyo 以外でもステータスバーの色を変更できます。

まとめ

TFMXViewController.SetStatusBarBackgroundColor が元々あったのは、今回の TCommonCustomForm.SystemStatusBar への布石だったのかも。

2
1
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
2
1