iOS 7 Navigationbar background image issue

╄→гoц情女王★ 提交于 2019-12-13 12:12:59

问题


I am using Image as Navigation bar background Image. To set Image I used following code:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_logo_ios7.png"] forBarMetrics:UIBarMetricsDefault];

For iOS7 "nav_logo_ios7.png" image size is 768x64 and for iOS6 and bellow I used image has size 768x44.

This is working well on all UIViewControllers.

In same project I am using UIActivityViewController. On iOS7 mail compose view look like this:

How I can handle this?

Thanks in advance.


回答1:


The issue you are facing is that when a UIViewController is presented modally, the status bar is not included in the height of the UINavigationBar.

This means that the 64pt image is incorrect.

First of all, the official and better way to check what version of iOS the device is running would be to do something like this:

if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1)
{
    //handle iOS 7 Stuff
}
else
{
    //handle older iOS versions
}

For more information, check out the NSObjCRuntime.h header.

UINavigationBar background images shouldn't really be a fixed size image and instead should be stretchable image such as a repeatable pattern so maybe it would be an idea to rethink future designs... However if you do want to continue with a custom fixed sized image then I have a suggestion for you...

The UINavigationController allows you to initialise an instance with custom UINavigationBar and UIToolbar classes using initWithNavigationBarClass:toolbarClass:... This means that you could init any views that you are not presenting modally with a different UINavigationBar subclass to views that are being modally presented.

This means that you will be able to specify different background images dependant on if your navigation controller is modally presented or not, for example:

UIImage *backgroundImage44pts = [UIImage imageNamed:@" ... "];
UIImage *backgroundImage64pts = [UIImage imageNamed:@" ... "];

if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1)
{
    //handle iOS 7 Stuff
    [[UINavigationBar appearance] setBackgroundImage:backgroundImage44pts forBarMetrics:UIBarMetricsDefault];
    [[UINavigationBarSubclass appearance] setBackgroundImage:backgroundImage64pts forBarMetrics:UIBarMetricsDefault];
}
else
{
    //handle older iOS versions
    [[UINavigationBar appearance] setBackgroundImage:backgroundImage44pts forBarMetrics:UIBarMetricsDefault];
}

One important thing to note is that the MFMailComposeViewController isn't a real view controller so trying to initialise it with custom navigation bar subclasses may not work.. That is why I have used a custom navigation bar subclass for all non-modal navigation controllers and not the other way round.

Another thing to note would be that if your application is universal then modal views do not exist (unless you have anything custom) and you would not have to worry about this.

As I said earlier... UINavigationBars aren't really designed to have fixed sized background images (this is why it is so difficult to achieve) so if you think this work around is too complicated then maybe it would be a good idea to rethink your design.

And one last thing (I promise)... One of the main design changes in iOS 7 is to have your content from the navigation bar flowing underneath the status bar.. Adding an image to prevent this and replace it with a solid white background seems rather strange for an iOS 7 app.




回答2:


//In `AppDelegate.m`

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
    {
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg.png"] forBarMetrics:UIBarMetricsDefault];


        [[UINavigationBar appearance] setTitleTextAttributes:
         @{
           UITextAttributeTextColor: [UIColor whiteColor],UITextAttributeTextShadowColor: [UIColor clearColor],UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],UITextAttributeFont: [UIFont fontWithName:@"ArialMT" size:18.0f]
        }];


        CGFloat verticalOffset = -4;
        [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
    }
    else
    {
        [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];

        // Uncomment to change the color of back button
        [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

        // Uncomment to assign a custom backgroung image
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg_ios7.png"] forBarMetrics:UIBarMetricsDefault];

        // Uncomment to change the back indicator image

        [[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@""]];
        [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@""]];

        // Uncomment to change the font style of the title

        NSShadow *shadow = [[NSShadow alloc] init];
        shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
        shadow.shadowOffset = CGSizeMake(0, 1);

        [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,shadow, NSShadowAttributeName,[UIFont fontWithName:@"ArialMT" size:18.0], NSFontAttributeName, nil]];

        CGFloat verticalOffset = -4;
        [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
    }

self.window.rootViewController = self.navigationController;

    [self.window makeKeyAndVisible];
    return YES;
}


来源:https://stackoverflow.com/questions/21854588/ios-7-navigationbar-background-image-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!