AvaloniaRootViewController.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using Avalonia.Media;
  2. using CoreGraphics;
  3. using UIKit;
  4. namespace Avalonia.iOS
  5. {
  6. class AvaloniaRootViewController : UIViewController
  7. {
  8. private object _content;
  9. private Color _statusBarColor = Colors.White;
  10. public object Content
  11. {
  12. get { return _content; }
  13. set
  14. {
  15. _content = value;
  16. var view = (View as AvaloniaView);
  17. if (view != null)
  18. view.Content = value;
  19. }
  20. }
  21. public Color StatusBarColor
  22. {
  23. get { return _statusBarColor; }
  24. set
  25. {
  26. _statusBarColor = value;
  27. var view = (View as AvaloniaView);
  28. if (view != null)
  29. view.BackgroundColor = value.ToUiColor();
  30. }
  31. }
  32. void AutoFit()
  33. {
  34. var needFlip = !UIDevice.CurrentDevice.CheckSystemVersion(8, 0) &&
  35. (InterfaceOrientation == UIInterfaceOrientation.LandscapeLeft
  36. || InterfaceOrientation == UIInterfaceOrientation.LandscapeRight);
  37. // Bounds here (if top level) needs to correspond with the rendertarget
  38. var frame = UIScreen.MainScreen.Bounds;
  39. if (needFlip)
  40. frame = new CGRect(frame.Y, frame.X, frame.Height, frame.Width);
  41. ((AvaloniaView) View).Padding =
  42. new Thickness(0, UIApplication.SharedApplication.StatusBarFrame.Size.Height, 0, 0);
  43. View.Frame = frame;
  44. }
  45. public override void LoadView()
  46. {
  47. View = new AvaloniaView() {Content = Content, BackgroundColor = _statusBarColor.ToUiColor()};
  48. UIApplication.Notifications.ObserveDidChangeStatusBarOrientation(delegate { AutoFit(); });
  49. UIApplication.Notifications.ObserveDidChangeStatusBarFrame(delegate { AutoFit(); });
  50. AutoFit();
  51. }
  52. }
  53. }