在iPhone底部添加StatusBar

时间:2012-05-15 11:01:33

标签: iphone ios uikit uiapplication

我想在iPhone底部显示一个状态栏,就像Gmail帐户中的状态栏一样,表明它正在检查邮件。我在这个帖子中尝试了以下解决方案 Adding view on StatusBar in iPhone

但状态栏没有出现,然后我使用相同的代码而没有任何修改,将其显示在默认状态栏的顶部,也没有出现。

我尝试了另一种使用MTStatusBarOverlay的解决方案,当我试图将其框架更改为底部时,我在屏幕中间有一个黑色矩形

任何帮助?

这是代码

// new class i have created
@interface BottomStatusBarOverlay :  UIWindow

@end

@implementation BottomStatusBarOverlay
- (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];
        self.alpha = 1;
        self.hidden = NO;
        // 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:@"statusBarBackgroundGrey.png"] stretchableImageWithLeftCapWidth:2.0f topCapHeight:0.0f];
        [self addSubview:backgroundImageView];
    }
    return self;
}
@end

// usage in my view controller in a button action
@implementation MainViewController
-(IBAction)showBottomStatusbar:(id)sender {
    BottomStatusBarOverlay *bottomStatusBarOverlay = [[BottomStatusBarOverlay alloc] init];
    bottomStatusBarOverlay.hidden = NO;
}

1 个答案:

答案 0 :(得分:1)

您发布的代码显示您的showBottomStatusbar方法创建了BottomStatusBarOverlay实例,但您实际上从未将其作为子视图添加到任何内容。

我不在iPhone上使用Gmail应用。所以,我不确定它的外观或功能如何。但是,我在过去创建了一个类似于您描述的通知栏。它会动画到屏幕底部,显示三秒钟的消息,然后向后滑动。我通过将栏添加到应用程序窗口来完成此操作,这将确保它覆盖应用程序当前显示的任何视图。但是,如果您的应用中不需要全局栏,则可以将栏添加到当前处于活动状态的任何视图中。以下是您获取应用程序窗口参考的方式:

UIApplication* app = [UIApplication sharedApplication];
UIWindow* appWin = app.delegate.window;

要设置动画,您可以使用animateWithDuration,如下所示:

[UIView animateWithDuration:0.3 
                 animations:^ { 
                     // However you want to animate on to the screen.
                     // This will slide it up from the bottom, assuming the
                     // view's start position was below the screen.
                     view.frame = CGRectMake(0, 
                                             winHeight - viewHeight, 
                                             winWidth, 
                                             viewHeight);
                 }
                 completion:^(BOOL finished) {
                     // Schedule a timer to call a dismiss method after
                     // a set period of time, which would probably perform
                     // an animation off the screen.
                     dismissTimer = [NSTimer 
                                     scheduledTimerWithTimeInterval:3
                                     target:globalMessage
                                     selector:@selector(dismiss)
                                     userInfo:nil
                                     repeats:NO];
                 }];

希望这有帮助。