我知道听起来这个问题的答案很简单,但请听我说完。虽然UIStatusBar
是 的子类UIView
,但您不能使用该addSubview
方法向其添加子视图,因为它不使用它。也是如此UIStatusBarWindow
。视图或窗口都没有视图控制器,所以我不能以任何方式挂钩。
这是代码的相关部分。addSubviews
我在 self 上调用该方法的那一行是问题所在,因为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
视图也会被隐藏。每当状态栏被隐藏时,我都可以隐藏它,但由于不同的应用程序使用了许多不同的隐藏状态栏的方法,结果不如我希望的那样好。