9

我知道听起来这个问题的答案很简单,但请听我说完。虽然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视图也会被隐藏。每当状态栏被隐藏时,我都可以隐藏它,但由于不同的应用程序使用了许多不同的隐藏状态栏的方法,结果不如我希望的那样好。

4

1 回答 1

2

我认为这里的解决方案是使用状态栏高度变化时传递的通知。

使用其中一个/两个:

UIApplicationWillChangeStatusBarFrameNotification

UIApplicationDidChangeStatusBarFrameNotification

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

-application:willChangeStatusBarFrame:

-application:didChangeStatusBarFrame:

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

于 2018-07-20T23:58:28.727 回答