的状态栏上添加视图

的状态栏上添加视图

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

问题描述

是否可以在大小为 (320 x 20) 的状态栏上添加 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 的状态栏上添加视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:28