如何在Theos中向UIStatusBar添加子视图?

时间:2022-09-06 20:22:15

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.

我知道听起来这个问题有一个简单的答案,但是听我说。尽管UIStatusBar是UIView的子类,但您不能使用addSubview方法向其添加子视图,因为它不使用它。 UIStatusBarWindow也是如此。视图或窗口都没有viewcontroller,所以我无法以任何方式挂钩。

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.

这是代码的相关部分。我在自己上调用addSubviews方法的行是问题,因为addSubviews不是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.

还有其他方法可以添加子视图吗?我试图这样做的原因是,只要状态栏被隐藏,我的roundedCorners视图也会被隐藏。每当状态栏被隐藏时我都可以隐藏它,但是由于不同的应用程序使用了许多不同的隐藏状态栏的方法,这种方法不能像我希望的那样有效。

1 个解决方案

#1


2  

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:

或者您也可以使用状态栏更改框架时调用的AppDelegate方法:

-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!

您可以在这些方法中根据状态栏的新框架调整圆角。希望这能解决你的问题!

#1


2  

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:

或者您也可以使用状态栏更改框架时调用的AppDelegate方法:

-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!

您可以在这些方法中根据状态栏的新框架调整圆角。希望这能解决你的问题!