Adding image to navigation bar

后端 未结 9 2071
一个人的身影
一个人的身影 2020-12-23 02:09

I\'d like an image to take up all of a navigation bar. This is the navigation that comes with a navigation based app. It appears on the RootViewController with the accompa

相关标签:
9条回答
  • 2020-12-23 02:40

    its changed for ios6, to make it work in ios 6 use:

    UINavigationBar *navBar = self.navigationController.navigationBar;
    UIImage *image = [UIImage imageNamed:@"image.png"];
    [navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    
    0 讨论(0)
  • 2020-12-23 02:45

    There is actually a much easier way to add a background image to any UIView class or subclass. It requires no class categorization or extension (subclassing), and you can do this on an "as needed" basis. For example, to add a background image to a view controller's navigation bar, do the following:

    self.navigationController.navigationBar.layer.contents = (id)[UIImage 
        imageNamed:@"background.png"].CGImage;
    

    You'll need to remember to add the Quartz Core framework to your project and add #import <QuartzCore/QuartzCore.h> wherever you need to do this. This is a much cleaner, simpler way to alter the drawing layer of anything that inherits from UIView. Of course, if you want to accomplish a similar effect for all navigation bars or tab bars, then subclassing makes sense.

    0 讨论(0)
  • 2020-12-23 02:46
    UIImage *logo = [UIImage imageNamed:@"my_logo"];
    UIImageView *logoView = [[UIImageView alloc]initWithImage:logo];
    logoView.frame = CGRectMake(0, 0, 320, 37);
    
    UINavigationController *searchNavCtrl = [[UINavigationController alloc] initWithRootViewController:searchViewController];
    searchNavCtrl.navigationBar.barStyle = UIBarStyleBlack;
    
    
    //searchNavCtrl.navigationItem.titleView = logoView;
    //[searchNavCtrl.navigationController.navigationBar.topItem setTitleView:logoView];
    [searchNavCtrl.navigationBar addSubview:logoView];
    
    [logoView release];
    
    0 讨论(0)
  • 2020-12-23 02:55

    In your case, this solution found in another answer would work well.

    With the "CustomImage" category added to UINavigationBar, you can then just call:

    UINavigationBar *navBar = self.navigationController.navigationBar;
    UIImage *image = [UIImage imageNamed:@"yourNavBarBackground.png"];
    [navBar setBackgroundImage:image];
    

    This code should go in the method

    - (void)viewWillAppear:(BOOL)animated
    

    of the view controller where you want to have the custom image. And, in that case you should better call:

    [navBar clearBackgroundImage]; // Clear any previously added background image
    

    before setBackgroundImage (otherwise it will be added multiple times...)

    0 讨论(0)
  • 2020-12-23 03:00

    Add code in appdelegate did finish with launching method

    #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
    
    if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
    {
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation_or.png"] forBarMetrics:UIBarMetricsDefault];
        [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault];
    }
    else
    {
        [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)];
    
        [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    
        // Uncomment to assign a custom backgroung image
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation_or.png"] forBarMetrics:UIBarMetricsDefault];
    
        // Uncomment to change the back indicator image
        [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
    
        [[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
        [[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, 0);
    
        [[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:@"HelveticaNeue-Bold" size:17], NSFontAttributeName, nil]];
    
        [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault];
    }
    
    0 讨论(0)
  • 2020-12-23 03:03
    UIImage *image = [UIImage imageNamed:@"YourImage.png"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    
    [self.navigationController.navigationBar addSubview:imageView];
    
    0 讨论(0)
提交回复
热议问题