Controlling StatusBar on iPhone X
Hi,
I have a game, in which all earlier versions of iPhone (3 - 8) is build without the Statusbar, but now with the iPhone X, after altering my UI to fit the iPhone X screen has 2 blank spots in the top.
Instead of leaving them blank, it would be nice to activate the statusbar to fill them out.
This means that I have to do this by script and in runtime.
I have looked at the UnityPlayer.PlayerSettings.statusBarHidden function, but this will not work :-/
I found this post, but am not able to make it work: https://answers.unity.com/questions/688922/hideshow-status-bar-androidios-on-runtime.html
Here is what I have tried:
Added this to plugins -> ios in my unity project:
StatusBar.mm
#import "StatusBar.h"
@implementation StatusBar
+ (void) hideStatusBar: (BOOL)hide
{
[[UIApplication sharedApplication] setStatusBarHidden: hide
withAnimation: UIStatusBarAnimationSlide];
[UnityGetGLViewController() setNeedsStatusBarAppearanceUpdate];
}
@end
extern "C"
{
void StatusBarSwitcherPressed(bool isHide)
{
[StatusBar hideStatusBar: isHide];
}
}
StatusBar.h
#import <Foundation/Foundation.h>
@interface StatusBar : NSObject
{
// Keeps track of available services
NSMutableArray *services;
// Keeps track of search status
NSString* status;
BOOL searching;
}
@end
I then added this c# file inside the plugin folder:
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class StatusBarCtrl {
[DllImport ("__Internal")]
private static extern void StatusBarSwitcherPressed (bool isHide);
public static void StausBarHide(bool isHide)
{
// Call plugin only when running on real device
if (Application.platform != RuntimePlatform.OSXEditor)
StatusBarSwitcherPressed(isHide);
}
}
And then calls this when I have figured out if its an iPhone X or not:
StatusBarCtrl.StausBarHide (false);
Hope this makes sense. When running this, nothing happens whether I set it to true or false :-/
Any help is appreciated and thanks in advance :-)
Answer by Johannski · Jan 10, 2018 at 05:31 PM
Take a look at this repo: https://github.com/HuaYe1975/UnityStatusBarForiOS
They somehow override functionality from #include "UnityViewControllerBaseiOS.h" if I got it right. Tested it with iOS 11, Unity 5.6.4p4 and Xcode9 and it works like a charm with that setup. If you use Unity 2017.2 or higher you will need to replace #include "UnityViewControllerBaseiOS.h" with #include "UnityViewControllerBase.h" in StatusBarViewController.mm.
You can easily check if the device is iPhoneX (e.g. Device.generation) and then set the status bar accordingly.
It works like a charm. I just added it without any corrections and I am using Unity 2017+. Thanks, just saved the day ;-)
Awesome! I was also really happy when I found the repo yesterday. I guess then the change is only needed from 2017.2 and up, changed it in the answer :)
This now produces many errors in Xcode 10.1, built with Unity 2018.3. $$anonymous$$ostly "Expected identifier" errors.
Answer by paradizIsCool · Jan 05, 2018 at 03:14 PM
You can try to follow method 1 here https://stackoverflow.com/questions/46677240/show-status-bar-only-for-iphone-x
confirmed You also can checked Device.generation != DeviceGeneration.iPhoneX
if you want to show status bar only on iphone X
@paradizlsCool The $$anonymous$$ethod described is for Swift, but appdelegate from Unity is not in Swift, so unfortunally I do not know how to implement this in Unity :-/ But thanks for the answer.
Also, in regards to detecting if its an iPhoneX inside Unity is not the issue. the issue is that I am unable to control the statusbar from within Unity when building ti iOS.
if you target ios8 you can use old method. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; https://developer.apple.com/documentation/uikit/uiapplication/1622923-setstatusbarstyle
Your answer
![](https://koobas.hobune.stream/wayback/20220612143450im_/https://answers.unity.com/themes/thub/images/avi.jpg)