Navigation bar issue iphone 4.3
-
29-10-2019 - |
Question
I am now developing an app which support both iOS 4.3 and 5.0.
My code works fine with 5.0, but results in a tricky-bug in 4.3.
Issue is:
I've got a view with a tableView in it. This view have a navigation bar with both left and right navigation bar items. Once I select the row in table view (and reaches it's corresponding view) and returns back, both the left and right navigation-bar items "vanish".
I've been over this mess for last few hours, some one got a cure?
This is what I have already done:
This is the utility method that I call when view loads.
+ (void)setNavigationBarContents:(UIViewController *)view
{
UIImage *topBarimage=[UIImage imageNamed:NAVIGATION_BAR_IMAGE];
if([view.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
[view.navigationController.navigationBar setBackgroundImage:topBarimage forBarMetrics:UIBarMetricsDefault];
}
else
{
[view.navigationController.navigationBar insertSubview:[[UIImageView alloc] initWithImage:topBarimage] aboveSubview:view.navigationController.navigationBar];
}
UIImage *logo=[UIImage imageNamed:LOGO];
UIImageView *logoView=[[UIImageView alloc]initWithImage:logo];
view.navigationItem.titleView = logoView;
UIImage *settingsButtonImage= [UIImage imageNamed:NAVIGATION_SETTINGS];
UIButton *rightBarButton = [UIButton buttonWithType: UIButtonTypeCustom];
[rightBarButton setBackgroundImage: settingsButtonImage forState:UIControlStateNormal];
[rightBarButton addTarget: view action:@selector(settingsButton:) forControlEvents:UIControlEventTouchUpInside];
rightBarButton.frame = CGRectMake(0, 0, 50, 40);
view.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: rightBarButton];
UIImage *logoutButtonImage= [UIImage imageNamed:LOGOUT];
UIButton *leftBarButton = [UIButton buttonWithType: UIButtonTypeCustom];
[leftBarButton setBackgroundImage: logoutButtonImage forState:UIControlStateNormal];
[leftBarButton addTarget: view action:@selector(logout:) forControlEvents:UIControlEventTouchUpInside];
leftBarButton.frame = CGRectMake(0, 0, 65, 32);
view.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: leftBarButton];
}
Have tried calling this both in viewDidLoad and viewWillAppear, but in vain.
This is how I call this in viewController.
- (void)viewDidLoad
{
[super viewDidLoad];
//other setups here.
[Utility setNavigationBarContents:self];
}
Solution
I got the solution by adding This code in my appDelegate.m
// added so that navigation bar background image works in version lower than 5.0
@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIImage *img = [UIImage imageNamed:NAVIGATION_BAR_IMAGE];
[img drawInRect:rect];
}
@end