代码之家  ›  专栏  ›  技术社区  ›  Biranchi

在iPhone中添加状态栏视图

  •  62
  • Biranchi  · 技术社区  · 15 年前

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

    5 回复  |  直到 10 年前
        1
  •  87
  •   alleus    10 年前

    您可以通过在现有状态栏上方创建自己的窗口来轻松完成这一点。

    只需创建 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 或类似的。如果你把主窗口 uiwindows 在应用程序委托)松散键状态中,当点击状态栏等时,您将遇到滚动视图到顶部的问题。

        2
  •  58
  •   myell0w    14 年前

    我编写了一个模拟Reeders状态栏覆盖的静态库,您可以在这里找到它: https://github.com/myell0w/mtstatusbaroverlay

    它目前支持iphone和ipad,默认和不透明的黑色状态栏样式,旋转,3种不同的任意模式,历史跟踪和更多的好东西!

    请随意使用它或发送一个拉请求来增强它!

    可以在这里找到: https://github.com/myell0w/MTStatusBarOverlay

    MTStatusBarOverlay MTStatusBarOverlay

    它目前支持iphone和ipad,默认和不透明的黑色状态栏样式,旋转,3种不同的任意模式,历史跟踪和更多的好东西!

    请随意使用它或发送一个拉请求来增强它!

        3
  •  4
  •   Sergey Kopanev    12 年前

    所有的答案看起来都有效,但在ios6.0中,我有下一个问题:

    1/旋转看起来不好

    2/窗口(状态栏是一种窗口)需要rootviewcontroller

    我使用的答案来自 迈尔沃 但是旋转效果不好。我刚刚删除了一个额外的窗口,并使用AppDelegate中的uiwindow来实现状态栏。 可能此解决方案仅适用于一个uiviewcontroller应用程序…

    我通过以下方式实施:

    1/在应用程序中委派:

    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)上都可以正常工作。

        4
  •  3
  •   Community CDub    8 年前

    只是为了驳回“你不能做这些评论”……

    我不知道怎么做,但我知道这是可行的。被称为Reeder的feed reader应用程序可以做到这一点。

    从屏幕截图中可以看到,Reeder在屏幕右上方放置了一个小圆点。当你点击它。该栏将填充整个状态栏,直到您再次点击该栏使其变小。

    被称为Reeder的人就是这样做的。

    从屏幕截图中可以看到,Reeder在屏幕右上方放置了一个小圆点。当你点击它。该栏将填充整个状态栏,直到您再次点击它使其变小。

    A small icon on the top right of the screen alt text

        5
  •  1
  •   Lefteris    12 年前

    首先,非常感谢@martin all_)我们为这个实现提供了代码。

    我只是在贴一个我所面临的问题和我使用的解决方案,因为我相信其他人可能也会遇到同样的问题。

    如果在调用发生时启动应用程序,状态栏高度将为40像素,这意味着自定义状态栏将用该高度初始化。 但是如果通话在你还在应用程序中的时候结束,状态栏的高度将保持在40像素,看起来会很奇怪。

    因此,解决方案很简单:我已经使用通知中心订阅了应用程序的状态栏框架更改委托,并调整了框架:

    - (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame {
        //an in call toggle was done    
        //fire notification
        [[NSNotificationCenter defaultCenter] postNotificationName:kStatusBarChangedNotification object:[NSValue valueWithCGRect:oldStatusBarFrame]];
    }
    

    在acstatusbaroverlaywindow中,我们订阅通知:

    -(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 只是一个我用来方便引用的常量,您可以简单地用字符串替换它,或者全局声明该常量。