Navigation Controller not working properly

2019-06-13 15:36发布

问题:

I am using a navigation controller in my current application, but I had an issue with the navigation controller with the iOS4 and iOS5 so i tried to write the code for both iOS 4 & 5

if([[UINavigationBar class] respondsToSelector:@selector(appearance)]) //iOS >=5.0
{
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
else
{
    self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:@"header.png"].CGImage;
}

But problem is when I run my app on iOS 4 version my navigation Controller look like this.

please suggest me.

回答1:

UINavigationController *navControl;

In else part, try like this.

UINavigationBar *navBar = self.navControl.navigationBar;
   UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
            imgView.image = [UIImage imageNamed:@"header.png"];
            [navBar addSubview:imgView];
            [imgView release];


回答2:

Thank after a long search I tried this code which helped me.

import "ImageViewController.h"

@implementation UINavigationBar (CustomImage)

- (void)drawRect:(CGRect)rect {
    UIImage *image = [UIImage imageNamed:@"background.png"];
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end
implement this code in your .m file


@implementation ImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    UIImageView *backGroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
    [self.navigationController.navigationBar insertSubview:backGroundView atIndex:0];
    [backGroundView release];
}

@end