33

ボタンがクリックされたときにステータス バーをスライドアウト (または非表示) にする必要があるビュー コントローラーを考えてみましょう。

- (void) buttonClick:(id)sender
{
    [[UIApplication sharedApplication] setStatusBarHidden:YES
                                            withAnimation:UIStatusBarAnimationSlide];
}

上記はステータスバーを効果的に非表示にしますが、ルートビューのサイズを適切に変更せず、上部に 20 ピクセルのギャップを残します。

私が期待していたのは、ルート ビューが、ステータス バーによって以前に使用されていたスペース (ステータス バーのアニメーションと同じ長さのアニメーション) に拡張されることです。

これを行う適切な方法は何ですか?

(同様の質問がたくさんあることは承知していますが、ステータスバーを非表示にして新しいView Controllerを表示するのではなく、オンデマンドでステータスバーを非表示にすることについては何も見つかりませんでした)

「ブルートフォース」アプローチ

明らかに、次の作品...

[[UIApplication sharedApplication] setStatusBarHidden:YES
                                        withAnimation:UIStatusBarAnimationSlide];
[UIView animateWithDuration:0.25 animations:^{
    CGRect frame = self.view.frame;
    frame.origin.y -= 20;
    frame.size.height += 20;
    self.view.frame = frame;
}];

...しかし欠点があります:

  • スライド アニメーションの長さをハードコードします
  • ステータスバーの高さをハードコードします
  • ルート ビューの原点は (0,-20) のままです。フレームは可能な限り (0,0) から開始するようにしています。

私がすでに試したこと

  • ルート ビューの自動サイズ変更マスクに と があることを確認しましUIViewAutoresizingFlexibleTopMarginUIViewAutoresizingFlexibleHeight
  • [self.view setNeedsLayout]ステータス バーを非表示にした後に呼び出されます。
  • [self.view setNeedsDisplay]ステータス バーを非表示にした後に呼び出されます。
  • ステータスバーを非表示にする前後に設定wantsFullScreenLayoutします。YES
4

7 に答える 7

27

ビュー コントローラー ベースのステータス バーの外観でこれを実装しようとしている場合は、ビュー コントローラーに prefersStatusBarHidden メソッドを実装する必要があります。

 - (BOOL)prefersStatusBarHidden
{
    // If self.statusBarHidden is TRUE, return YES. If FALSE, return NO.
    return (self.statusBarHidden) ? YES : NO;
}

そして、あなたのボタンクリックメソッドで:

- (void) buttonClick:(id)sender
{
    // Switch BOOL value
    self.statusBarHidden = (self.statusBarHidden) ? NO : YES;

    // Update the status bar
    [UIView animateWithDuration:0.25 animations:^{
        [self setNeedsStatusBarAppearanceUpdate];
    }];
}

アニメーション スタイルを設定するには、次を使用します。

-(UIStatusBarAnimation)preferredStatusBarUpdateAnimation
{
    return UIStatusBarAnimationSlide;
}

スタイルをカスタマイズするには:

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
于 2015-11-14T23:14:53.627 に答える
15

これは問題なく動作し、何もハード コードされていません。

CGRect appFrame = [[UIScreen mainScreen] applicationFrame];

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
[UIView animateWithDuration:0.25 animations:^{
    self.navigationController.navigationBar.frame = self.navigationController.navigationBar.bounds;
    self.view.window.frame = CGRectMake(0, 0, appFrame.size.width, appFrame.size.height);
}];
于 2012-11-29T13:05:15.013 に答える
8

ステータスバーを正しく非表示にするために、モーダルビューコントローラーを表示してから閉じることができます

- (void)toggleStatusBar {
    BOOL isStatusBarHidden = [[UIApplication sharedApplication] isStatusBarHidden];
    [[UIApplication sharedApplication] setStatusBarHidden:!isStatusBarHidden];

    UIViewController *vc = [[UIViewController alloc] init];
    [self presentViewController:vc animated:NO completion:nil];
    [self dismissViewControllerAnimated:NO completion:nil];
    [vc release];
}

横向きの「willAnimateRotationToInterfaceOrientation」メソッドでこのコードを使用しましたが、すべてが正しく機能しています。しかし、それがアニメーションで機能するかどうかはわかりません。

于 2013-05-23T09:16:49.597 に答える
7

ビューのサイズも変更するステータスバーを非表示または表示します。

-(void)statusBar:(BOOL)status {
UIViewController *rootViewController = self.view.window.rootViewController;
UIView *view = rootViewController.view;

// Hide/Unhide the status bar
[[UIApplication sharedApplication] setStatusBarHidden:status]; // BOOL : YES or NO

// statusBar frame
CGRect statusBarFrame = [UIApplication.sharedApplication statusBarFrame];
// Establish baseline frame
CGRect newViewFrame = self.view.window.bounds;

// Check statusBar frame is worth dodging
if (!CGRectEqualToRect(statusBarFrame, CGRectZero)) {
    UIInterfaceOrientation currentOrientation = rootViewController.interfaceOrientation;
    if (UIInterfaceOrientationIsPortrait(currentOrientation)) {
        // If portrait need to shrink height
        newViewFrame.size.height -= statusBarFrame.size.height;
        if (currentOrientation == UIInterfaceOrientationPortrait) {
            // If not upside-down move down origin
            newViewFrame.origin.y += statusBarFrame.size.height;
        }
    } else { // Is landscape 
        // portrait shrink width
        newViewFrame.size.width -= statusBarFrame.size.width;
        if (currentOrientation == UIInterfaceOrientationLandscapeLeft) {
            // If the status bar is on the left side of the window move origin
            newViewFrame.origin.x += statusBarFrame.size.width;
        }
    }
}
view.frame = newViewFrame; // pass new frame 
}

呼び出し方法 (メッセージ):

 if ([[UIApplication sharedApplication] isStatusBarHidden]) {
        [self statusBar:NO];
 } else {
        [self statusBar:YES];
 }
于 2013-04-30T15:46:42.500 に答える
0

私はこれを回避する方法を知っていますが、欠点も明らかです。xibファイルを画面と同じ大きさに設定できself.wantsFullScreenLayout = YES;ますviewDidLoad(iPhone5の場合は320x480、および320x568)。ただし、これはステータス バーの下の領域も表示されないことを意味します。また、この方法を使用すると、ステータス バーを非表示にしてもビューが拡張されません。ステータスバー領域に表示するものがなければ、このように考えることができます。

于 2012-11-29T12:00:34.793 に答える