在iPhone中的StatusBar上添加视图 [英] Adding view on StatusBar in iPhone

查看:161
本文介绍了在iPhone中的StatusBar上添加视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在尺寸为(320 x 20)的staus条上添加UIView?我不想隐藏状态栏,我只想将其添加到状态栏的顶部。

Is it possible to add a UIView on the staus bar of size (320 x 20)? I don't want to hide the status bar, I only want to add it on top of the status bar.

推荐答案

你通过在现有状态栏上方创建自己的窗口,可以轻松实现此目的。

You can easily accomplish this by creating your own window above the existing status bar.

只需创建 UIWindow 的简单子类使用以下覆盖 initWithFrame:

Just create a simple subclass of UIWindow with the following override of 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

您现在可以,例如在视图控制器中在你的应用程序中,创建一个新类的实例并使其可见。

You can now, for example in a view controller in your application, create an instance of your new class and make it visible.

overlayWindow = [[ACStatusBarOverlayWindow alloc] initWithFrame:CGRectZero];
overlayWindow.hidden = NO;

请注意使用搞乱窗口密钥状态 - (void) makeKeyAndVisible 或类似的。如果您使主窗口(应用程序委托中的 UIWindow )松开键状态,则在点击状态栏等时将滚动浏览器滚动到顶部会遇到问题。

Be aware of messing with the window key status by using - (void)makeKeyAndVisible or similar. If you make your main window (the UIWindow in your Application Delegate) loose key status, you will encounter problems with scrolling scrollviews to top when tapping the status bar etc.

这篇关于在iPhone中的StatusBar上添加视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆