62

サイズ (320 x 20) のステータス バーに UIView を追加することは可能ですか? ステータスバーを非表示にしたくありません。ステータスバーの上に追加したいだけです。

4

5 に答える 5

87

これは、既存のステータスバーの上に独自のウィンドウを作成することで簡単に実行できます。

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

これで、たとえばアプリケーションのView Controllerで、新しいクラスのインスタンスを作成して表示できるようになります。

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

を使用するなどして、ウィンドウキーのステータスをいじることに注意してください- (void)makeKeyAndVisible。メインウィンドウ(UIWindowアプリケーションデリゲート内)のキーステータスを緩めると、ステータスバーなどをタップしたときにスクロールビューを上にスクロールする際に問題が発生します。

于 2010-05-14T12:22:59.760 に答える
58

Reeders のステータス バー オーバーレイを模倣した静的ライブラリを作成しました。ここで見つけることができます: https://github.com/myell0w/MTStatusBarOverlay

MTStatusBarOverlay MTStatusBarOverlay

現在、iPhone と iPad、デフォルトおよび不透明な黒のステータス バー スタイル、回転、3 つの異なるアニメーション モード、履歴追跡、その他多くの機能をサポートしています。

自由に使用するか、プル リクエストを送って機能を強化してください。

于 2010-12-10T23:51:58.997 に答える
4

すべての答えは機能しているように見えますが、iOS6.0 では次の問題があります。

1/ 回転が悪い

2/ ウィンドウ (ステータス バーはウィンドウの一種です) rootViewController が必要です

myell0wからの回答を使用していますが、回転がうまくいきません。余分なウィンドウを 1 つ削除し、AppDelegate から UIWindow を使用してステータス バーを実装しました。このソリューションは、1 つの 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;

コントローラー ビューには、コンテンツ ビューとステータス バー ビューの 2 つのビューがあります。ステータスバーを表示したり、必要に応じて非表示にしたりできます。コンテンツ ビューのフレームは次のようになります。

_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 で問題なく動作します。

于 2012-11-20T14:40:18.860 に答える
3

「このコメントはできません」を却下するだけです...

方法はわかりませんが、実行可能です。Reederと呼ばれるフィードリーダーアプリがそれを行います。

スクリーンショットからわかるように、Reederは画面の右上に小さな点を配置します。タップすると。もう一度タップして小さくするまで、バーはステータスバー全体に表示されます。

画面右上の小さなアイコン 代替テキスト

于 2010-05-14T11:58:48.580 に答える
1

まず、この実装のコードを提供してくれた @Martin Alléus に感謝します。

他の人も同じ問題を経験する可能性があると思うので、私が直面した問題と私が使用した解決策を投稿しているだけです。

通話中にアプリを起動すると、ステータス バーの高さは 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単純に文字列に置き換えるか、定数をグローバルに宣言できます。

于 2012-10-23T20:09:45.480 に答える