iCoder4777
iCoder4777

Reputation: 1692

UIBarButtonItem not showing in iOS 4.0

Everything is fine in iOS 5.0, but when i'm running my code in iOS 4.0, background image of bar button in navigation as well as title in navigation is not showing up.

Here is the code i'm using:

// check for version 5.0 support
navController  = [[[UINavigationController alloc] initWithRootViewController:self.viewController] autorelease];
UINavigationBar *navigationBar = [navController navigationBar];
if ([navController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
    UIImage *image = [UIImage imageNamed:@"Nav_Bar.png"];
    [navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}

else
{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"Nav_Bar.png"]];
    [self.navController.navigationBar addSubview:imageView];
}

Then in viewDidLoad:

self.title = @"MyTitle";

self.leftBTN=  [UIButton buttonWithType:UIButtonTypeCustom];
[leftBTN setImage:[UIImage imageNamed:@"BTN1.png"] forState:UIControlStateNormal];
[leftBTN setTitle:@"Edit" forState:UIControlStateNormal];
[leftBTN addTarget:self action:@selector(editOption) forControlEvents:UIControlEventTouchUpInside];
[leftBTN setFrame:CGRectMake(0, 0, 60, 30)];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:leftBTN] autorelease];

Can any one please, explain me, why image and title is not showing in navigation controller. Moreover, if I click on that frame, button’s action is working, only problem is that button is not showing.

Upvotes: 1

Views: 291

Answers (1)

Apurv
Apurv

Reputation: 17186

Its a problem with iOS 4. When the view is added to Navigation Bar, the subviews are not handled properly. Its resolved in iOS 5.

See this link.

It has sample project which runs with background image in navigation bar on both iOS 4 and iOS 5.

Upvotes: 1

Related Questions