能否在大小为(320 x 20)的状态栏上添加一个UIView?我不想隐藏状态栏,只想将其添加到状态栏顶部。
能否在大小为(320 x 20)的状态栏上添加一个UIView?我不想隐藏状态栏,只想将其添加到状态栏顶部。
你可以通过创建一个自己的窗口放在现有状态栏之上来轻松实现这一点。
只需创建一个简单的UIWindow
子类,并使用以下initWithFrame:
重写:
@interface ACStatusBarOverlayWindow : UIWindow {
}
@end
@implementation ACStatusBarOverlayWindow
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Place the window on the correct level and position
self.windowLevel = UIWindowLevelStatusBar+1.0f;
self.frame = [[UIApplication sharedApplication] statusBarFrame];
// Create an image view with an image to make it look like a status bar.
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:self.frame];
backgroundImageView.image = [UIImage imageNamed:@"statusBarBackground.png"];
[self addSubview:backgroundImageView];
[backgroundImageView release];
// TODO: Insert subviews (labels, imageViews, etc...)
}
return self;
}
@end
现在你可以在应用程序的视图控制器中创建一个新类的实例并使其可见。
overlayWindow = [[ACStatusBarOverlayWindow alloc] initWithFrame:CGRectZero];
overlayWindow.hidden = NO;
要注意不要通过使用- (void)makeKeyAndVisible
或类似的方法来更改窗口键状态。如果使您的主窗口(应用程序代理中的UIWindow
)失去关键状态,则在轻击状态栏等操作时,您将遇到滚动滚动视图以返回顶部的问题。
请注意,不要使用- (void)makeKeyAndVisible
或类似的方法来更改窗口键状态。如果使您的主窗口(即应用程序委托中的UIWindow
)失去关键状态,那么在轻触状态栏等情况下,滚动滚动视图返回顶部可能会出现问题。
我编写了一个静态库,类似于Reeders状态栏覆盖层,您可以在这里找到它:https://github.com/myell0w/MTStatusBarOverlay
目前它支持iPhone和iPad、默认和不透明黑色状态栏风格、旋转、3种不同的动画模式、历史跟踪和更多好东西!
欢迎使用它或向我发送Pull请求以增强它!
所有的答案看起来都可以运行,但在iOS6.0中我遇到了以下问题:
1/ 旋转效果不佳
2/ 窗口(状态栏就像窗口一样)需要rootViewController
我使用了myell0w的答案,但旋转效果不太好。我只是删除了一个额外的窗口,并使用AppDelegate中的UIWindow来实现状态栏。也许这种解决方案只适用于一个UIViewController-app...
我是这样实现的:
1/ 在ApplicationDelegate中:
self.window.windowLevel = UIWindowLevelStatusBar + 1;
self.window.backgroundColor = [UIColor clearColor];
self.window.rootViewController = _journalController;
2/ 创建自定义UIView并在其中实现所有需要的内容: 例如可触摸状态栏:
@interface LoadingStatusBar : UIControl
轻松创建并添加到您的控制器视图中:
_loadingBar = [[LoadingStatusBar alloc] initWithFrame:topFrame];
[self addSubview:_loadingBar];
3/ 在 initWithFrame: 中添加控制器视图时需要注意一些技巧。
CGRect mainFrame = self.bounds;
mainFrame.origin.y = 20;
self.bounds = mainFrame;
您的控制器视图将有两个视图 - 内容视图和状态栏视图。您可以在需要时显示状态栏,也可以隐藏它。 内容视图的框架将为:
_contentView.frame = CGRectMake(0, 20, self.bounds.size.width, self.bounds.size.height);
4/ 最后一个神奇的技巧 :) 为了检测非可触摸区域的触摸,我使用了:
-(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if (point.y < 20) return _loadingBar;
return [super hitTest:point withEvent:event];
}
目前它在iPad/iPhone上运行良好,并且支持iOS 4到6。
仅是为了驳斥"您无法做到这一点的评论"...
我不知道怎么做,但我知道它是可行的。名为Reeder的Feed阅读器应用程序可以实现。
从屏幕右上角的小圆点可以看出,Reeder会在屏幕右上角放置一个小圆点。当您点击它时,该条将填满整个状态栏,直到再次点击它使其变小。
- (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame {
//an in call toggle was done
//fire notification
[[NSNotificationCenter defaultCenter] postNotificationName:kStatusBarChangedNotification object:[NSValue valueWithCGRect:oldStatusBarFrame]];
}
-(id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
// Place the window on the correct level & position
self.windowLevel = UIWindowLevelStatusBar + 1.0f;
self.frame = [UIApplication sharedApplication].statusBarFrame;
self.backgroundColor = [UIColor blackColor];
//add notification observer for in call status bar toggling
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarChanged:) name:kStatusBarChangedNotification object:nil];
}
return self;
}
以及我们的代码来调整框架:
- (void)statusBarChanged:(NSNotification*)notification {
//adjust frame...
self.frame = [UIApplication sharedApplication].statusBarFrame;
//you should adjust also the other controls you added here
}
kStatusBarChangedNotification
只是我为了方便引用而使用的一个常量,你可以简单地将其替换为字符串,或者全局声明该常量。