This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class App : Application | |
{ | |
public App() | |
{ | |
InitializeComponent(); | |
MainPage = new MainPage(); | |
} | |
#if __IOS__ | |
private DisplayOrientation _lastOrientation; | |
protected override Window CreateWindow(IActivationState activationState) | |
{ | |
Window window = base.CreateWindow(activationState); | |
window.Created += (s, e) => | |
{ | |
UpdateStatusBarColor(); | |
}; | |
window.Destroying += (s, e) => | |
{ | |
NSNotificationCenter.DefaultCenter.RemoveObserver(new NSString("UIDeviceOrientationDidChangeNotification")); | |
}; | |
return window; | |
} | |
private void UpdateStatusBarColor() | |
{ | |
UIView statusBar; | |
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0)) | |
{ | |
int tag = 4567890; | |
UIWindow window = UIApplication.SharedApplication.Delegate.GetWindow(); | |
statusBar = window.ViewWithTag(tag); | |
if (statusBar == null || statusBar.Frame != UIApplication.SharedApplication.StatusBarFrame) | |
{ | |
statusBar = statusBar ?? new(UIApplication.SharedApplication.StatusBarFrame); | |
statusBar.Frame = UIApplication.SharedApplication.StatusBarFrame; | |
statusBar.Tag = tag; | |
window.AddSubview(statusBar); | |
} | |
} | |
else | |
{ | |
statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView; | |
} | |
if (statusBar != null) | |
{ | |
// TODO Make this color come from somewhere shared | |
statusBar.BackgroundColor = Color.FromArgb("#2B0B98").ToUIColor(); | |
} | |
NSNotificationCenter.DefaultCenter.AddObserver(new NSString("UIDeviceOrientationDidChangeNotification"), NotificationCenter => | |
{ | |
// This gets called multiple times on iOS, let's optimize a little bit | |
if (_lastOrientation != DeviceDisplay.MainDisplayInfo.Orientation) | |
{ | |
UpdateStatusBarColor(); | |
_lastOrientation = DeviceDisplay.MainDisplayInfo.Orientation; | |
} | |
}); | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment