Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a subview to UIStatusBar in Theos?

I know it sounds like this question has a simple answer, but hear me out. Although UIStatusBar is a subclass of UIView, you can't use the addSubview method to add a subview to it because it doesn't use it. The same goes for UIStatusBarWindow. Neither the view or window have a viewcontroller, so I can't hook into that in any way.

Here is the relevant section of code. The line where I call the addSubviews method on self is the issue, because addSubviews isn't a method of UIStatusBar.

#import <CoreGraphics/CoreGraphics.h>

@interface UIStatusBar : UIView
@end

%hook UIStatusBar
- (void)layoutSubviews {
  //Round corners under status bar
  CGFloat radius = 15;
  CGRect wholeScreen = [[UIScreen mainScreen] bounds];
  UIView *roundedCorners = [[UIView alloc] initWithFrame: CGRectMake(-radius, 20-radius, wholeScreen.size.width+2*radius, wholeScreen.size.height-20+2*radius)];
  roundedCorners.layer.borderWidth = radius;
  roundedCorners.layer.cornerRadius = 2*radius;
  roundedCorners.layer.borderColor = UIColor.blackColor.CGColor;
  roundedCorners.userInteractionEnabled = NO;
  [self addSubView:roundedCorners];
}
%end

Is there another way I can add the subview? The reason I'm trying to do it this way is so that, whenever the status bar is hidden, my roundedCorners view is also hidden. I could hide it whenever the status bar is hidden, but due to different apps using many different methods of hiding the status bar that doesn't work out as well as I hoped.

like image 646
Artillect Avatar asked Jul 17 '18 19:07

Artillect


1 Answers

I think a solution here is to use the notifications delivered whenever the status bar's height changes.

Using either/both:

UIApplicationWillChangeStatusBarFrameNotification

UIApplicationDidChangeStatusBarFrameNotification

or you can also use the AppDelegate methods that get called when the status bar changes frame:

-application:willChangeStatusBarFrame:

-application:didChangeStatusBarFrame:

You can in these methods adjust your rounded corners according to the status bar's new frame. Hopefully this solves you problem!

like image 91
BHendricks Avatar answered Oct 03 '22 09:10

BHendricks