FLEXWindow.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // FLEXWindow.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 4/13/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXWindow.h"
  9. #import <objc/runtime.h>
  10. @implementation FLEXWindow
  11. - (id)initWithFrame:(CGRect)frame
  12. {
  13. self = [super initWithFrame:frame];
  14. if (self) {
  15. // Some apps have windows at UIWindowLevelStatusBar + n.
  16. // If we make the window level too high, we block out UIAlertViews.
  17. // There's a balance between staying above the app's windows and staying below alerts.
  18. // UIWindowLevelStatusBar + 100 seems to hit that balance.
  19. self.windowLevel = UIWindowLevelStatusBar + 100.0;
  20. }
  21. return self;
  22. }
  23. - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
  24. {
  25. BOOL pointInside = NO;
  26. if ([self.eventDelegate shouldHandleTouchAtPoint:point]) {
  27. pointInside = [super pointInside:point withEvent:event];
  28. }
  29. return pointInside;
  30. }
  31. - (BOOL)shouldAffectStatusBarAppearance
  32. {
  33. return [self isKeyWindow];
  34. }
  35. - (BOOL)canBecomeKeyWindow
  36. {
  37. return [self.eventDelegate canBecomeKeyWindow];
  38. }
  39. + (void)initialize
  40. {
  41. // This adds a method (superclass override) at runtime which gives us the status bar behavior we want.
  42. // The FLEX window is intended to be an overlay that generally doesn't affect the app underneath.
  43. // Most of the time, we want the app's main window(s) to be in control of status bar behavior.
  44. // Done at runtime with an obfuscated selector because it is private API. But you shouldn't ship this to the App Store anyways...
  45. NSString *canAffectSelectorString = [@[@"_can", @"Affect", @"Status", @"Bar", @"Appearance"] componentsJoinedByString:@""];
  46. SEL canAffectSelector = NSSelectorFromString(canAffectSelectorString);
  47. Method shouldAffectMethod = class_getInstanceMethod(self, @selector(shouldAffectStatusBarAppearance));
  48. IMP canAffectImplementation = method_getImplementation(shouldAffectMethod);
  49. class_addMethod(self, canAffectSelector, canAffectImplementation, method_getTypeEncoding(shouldAffectMethod));
  50. // One more...
  51. NSString *canBecomeKeySelectorString = [NSString stringWithFormat:@"_%@", NSStringFromSelector(@selector(canBecomeKeyWindow))];
  52. SEL canBecomeKeySelector = NSSelectorFromString(canBecomeKeySelectorString);
  53. Method canBecomeKeyMethod = class_getInstanceMethod(self, @selector(canBecomeKeyWindow));
  54. IMP canBecomeKeyImplementation = method_getImplementation(canBecomeKeyMethod);
  55. class_addMethod(self, canBecomeKeySelector, canBecomeKeyImplementation, method_getTypeEncoding(canBecomeKeyMethod));
  56. }
  57. @end