Frage

Ich möchte einen größeren Hintergrund für meine Navigationsleiste festlegen, wenn der Bildschirm in den Landschaftsmodus umgestellt wird. Das habe ich also in meinem Ansichtscontroller gemacht:

- (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]]]];
}

Und das ist das createTopBar Methode:

+ (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;
}

Das Ergebnis wird im Porträtmodus gut ausgeführt:

enter image description here

Dies funktioniert auch hervorragend im Landschaftsmodus in iOS 6:

enter image description here

Aber so stellt es sich in iOS 7, Landschaftsmodus, heraus:

enter image description here

Sie können sehen, dass die Statusleiste die Navigationsleiste überlappt, und im unteren Rand der Navigationsleiste befindet sich zusätzlichen Platz. (Nebenbei bemerkt habe ich die Info.plist -Datei bearbeitet, um das Problem der überlappenden Statusleiste zu beheben. Dies geschieht nur, wenn ich versuche, ein neues Hintergrundbild für die NAV -Leiste festzulegen.) Haben Sie einen Vorschlag für dieses Problem? Wenn Sie dies tun, lassen Sie es mich bitte wissen und danke Ihnen.

War es hilfreich?

Lösung

Vielen Dank an @eagle.dan.1349s Antwort habe ich eine Idee: Ich erweitere die Höhe des Navigationsleistenhintergrund Für die Statusleiste:

+ (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;
}

Und Voila, es funktioniert wie ein Zauber! Ich hoffe wirklich, dass dies für jemanden, der versucht, den Status -Bar -Stil der iOS 6 wie ich zu halten, irgendwie hilfreich sein könnte.

Andere Tipps

iOS 7 haben ziemlich viel undokumentiertes Verhalten in Bezug auf Systemkontrollen und Aussehen. Ich schlage vor, Sie geben auf, gegen die Statusleiste zu kämpfen und Ihr Bild so gut zu passen. Sie können versuchen, nur 20px schwarze Farbe oben im Bild hinzuzufügen.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top