質問

画面がランドスケープモードに切り替えられたときに、ナビゲーションバーのより大きな背景を設定したいと思います。これが私が私のビューコントローラーで行ったことです:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    UIDeviceOrientation deviceOrientation = toInterfaceOrientation;

    if([[UINavigationBar class] respondsToSelector:@selector(appearance)]) {
        [self.navigationController.navigationBar setBackgroundImage: [UIImageHelper createTopBar: deviceOrientation] forBarMetrics: UIBarMetricsDefault];
    }
    else
        [self.navigationController.navigationBar setBackgroundColor: [UIColor colorWithPatternImage: [UIImageHelper createTopBar: [[UIDevice currentDevice] orientation]]]];
}

そしてこれがです createTopBar 方法:

+ (UIImage *)createTopBar: (UIDeviceOrientation) orientation {
    // Create a new image context
    CGSize size;
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight){
        if ([UIDevice isiPhone5]) {
            size = CGSizeMake(568, 34);
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(568, 34), NO, 0.0);
        }
        else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            size = CGSizeMake(1024, 44);
            UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
        }
        else {
            size = CGSizeMake(480, 34);
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(480, 34), NO, 0.0);
        }
    }
    else{
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            size = CGSizeMake(768, 44);
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(768, 44), NO, 0.0);
        }
        else if ([UIDevice isiPhone5]) {
            size = CGSizeMake(320, 44);
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 44), NO, 0.0);
        }
        else {
            size = CGSizeMake(320, 44);
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 44), NO, 0.0);
        }
    }
    UIImage * image = [UIImage imageNamed: @"top_bar_without_title"];

    [image drawInRect:CGRectMake(0, 0, size.width, size.height+4)];
    UIImage * destImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return destImage;
}

ポートレートモードでは、結果は問題が発生します。

enter image description here

これは、iOS 6のランドスケープモードでもうまく機能します。

enter image description here

しかし、これはiOS 7、ランドスケープモードでの方法です。

enter image description here

ステータスバーがナビゲーションバーと重複しているのを見ることができ、ナビゲーションバーの底にいくつかの余分なスペースがあります。 (サイドノートでは、info.plistファイルを編集して、重複するステータスバーの問題を修正しました。これは、NAVバーの新しい背景画像を設定しようとする場合にのみ発生します。)この問題について何か提案はありますか?もしそうなら、私に知らせてくれてありがとう。

役に立ちましたか?

解決

@eagle.dan.1349の回答のおかげで、アイデアを思いつきました。ナビゲーションバーの背景の高さを拡張してステータスバーの高さを含めてから、背景の画像を下から下からスペースを離れることを開始します。ステータスバーの場合:

+ (UIImage *)createTopBar: (UIDeviceOrientation) orientation {
    // Create a new image context
    CGSize size;
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight){
        if ([UIDevice isiPhone5]) {
            if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7){
                size = CGSizeMake(568, 34);
                UIGraphicsBeginImageContextWithOptions(CGSizeMake(568, 54), NO, 0.0);
            }
            else {
                size = CGSizeMake(568, 34);
                UIGraphicsBeginImageContextWithOptions(CGSizeMake(568, 34), NO, 0.0);
            }

        }
        else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            size = CGSizeMake(1024, 44);
            UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
        }
        else {
            if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7){
                size = CGSizeMake(480, 34);
                UIGraphicsBeginImageContextWithOptions(CGSizeMake(480, 54), NO, 0.0);
            }
            else {
                size = CGSizeMake(480, 34);
                UIGraphicsBeginImageContextWithOptions(CGSizeMake(480, 34), NO, 0.0);
            }

        }
    }
    else{
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            size = CGSizeMake(768, 44);
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(768, 44), NO, 0.0);
        }
        else if ([UIDevice isiPhone5]) {
            size = CGSizeMake(320, 44);
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 44), NO, 0.0);

        }
        else {
            size = CGSizeMake(320, 44);
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 44), NO, 0.0);

        }
    }
    UIImage * image = [UIImage imageNamed: @"top_bar_without_title"];

    if ((orientation == UIInterfaceOrientationLandscapeLeft ||
         orientation == UIInterfaceOrientationLandscapeRight) &&
        [[[UIDevice currentDevice] systemVersion] floatValue] >= 7){

        [[UIColor blackColor] set];
        UIRectFill(CGRectMake(0, 0, size.width, 40));
        [image drawInRect:CGRectMake(0, 20, size.width, size.height+4)];
    }
    else {
        [image drawInRect:CGRectMake(0, 0, size.width, size.height+4)];
    }
    UIImage * destImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return destImage;
}

そして出来上がり、それは魅力のように機能します!私のようなiOS 6のステータスバースタイルを保持しようとしている人にとって、これが何らかの形で役立つことを本当に願っています。

他のヒント

iOS 7には、システム制御と外観の点で、文書化されていない動作が非常に多くあります。ステータスバーと戦う「あきらめる」ことをお勧めし、その下に適合するように画像を調整します。画像の上部に20pxの黒色を追加するだけです。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top