문제

화면이 조경 모드로 전환 될 때 내비게이션 막대에 더 큰 배경을 설정하고 싶습니다. 이것이 제가 뷰 컨트롤러에서 한 일입니다.

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

그리고 Voila, 그것은 매력처럼 작동합니다! 나와 같은 iOS 6의 상태 바 스타일을 유지하려는 사람에게 이것이 도움이되기를 바랍니다.

다른 팁

iOS 7은 시스템 제어 및 외관 측면에서 문서화되지 않은 동작이 많이 있습니다. 상태 표시 줄과 싸우고 이미지를 잘 맞도록 조정하는 것이 좋습니다. 이미지 상단에 20px의 검은 색을 추가 할 수 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top