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

9
我知道这个问题似乎有一个简单的答案,但请听我说完。虽然UIStatusBarUIView的子类,但您不能使用addSubview方法将子视图添加到它,因为它不使用它。同样适用于UIStatusBarWindow。视图和窗口都没有视图控制器,所以我无法以任何方式连接到它。
以下是相关的代码片段。我在self上调用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

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

1个回答

2
我认为解决方法是使用每当状态栏高度更改时提供的通知。可以使用以下一种或两种方式:
{{link1:UIApplicationWillChangeStatusBarFrameNotification}}
{{link2:UIApplicationDidChangeStatusBarFrameNotification}}
或者,您也可以使用在状态栏更改框架时调用的AppDelegate方法:
{{link3:-application:willChangeStatusBarFrame:}}
{{link4:-application:didChangeStatusBarFrame:}}
您可以在这些方法中根据状态栏的新框架调整圆角。希望这能解决你的问题!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接