当屏幕切换到景观模式时,我想为导航栏设置更大的背景。因此,这就是我在视图控制器中所做的:

- (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的答案,我想出了一个想法:我将导航栏背景的高度扩展到包括状态栏的高度,然后开始从Lower绘制背景图像以离开空间对于状态栏:

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