I am adding image to navigation bar it works fine but when i want to add title
self.title=@\"Activity\";
it does not show anything
Instead of add Image in UINavigationBar
, set background Image using bellow code..
UINavigationBar *navBar = [[self navigationController] navigationBar];
UIImage *backgroundImage = [UIImage imageNamed:@"Nav.png"];
[navBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
and then set Title like bellow...
self.title=@"Activity";
UPDATE
if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
[navBar setBackgroundImage:[UIImage imageNamed:@"Nav.png"] forBarMetrics:UIBarMetricsDefault];
}
else
{
UIImageView *imageView = (UIImageView *)[navBar viewWithTag:1];//any tag
if (imageView == nil)
{
imageView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:@"Nav.png"]];
[navBar insertSubview:imageView atIndex:0];
[imageView release];
}
}
self.title=@"Activity";
if going for another way as adding subview to navigation bar then in viewWillDisAppear
method of view you should set the title hidden and on viewWillAppear
method set hidden false.i was facing same problem as yours.
so whenever you leaving your current view viewWillDisAppear
method will called and it will hide the current view's title and as you again in same view then viewWillAppear
method will display the title again.
You can simply hide default navigation bar. and add following code -
NavigationView = [[UIView alloc]init];
NavigationView.frame = CGRectMake(0, 0 , 320, 44);
UIImageView *TopBarImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
TopBarImg.image = [UIImage imageNamed:@"Nav.png"];
[NavigationView addSubview:TopBarImg];
TopBarImg.userInteractionEnabled = TRUE;
UILabel *TopTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
TopTitle.backgroundColor = [UIColor clearColor];
TopTitle.textAlignment = UITextAlignmentCenter;
TopTitle.text = @"Activity";
TopTitle.textColor=[UIColor whiteColor];
TopTitle.backgroundColor=[UIColor clearColor];
TopTitle.font=[UIFont fontWithName:@"Helvetica-Bold" size :18];
[TopBarImg addSubview:TopTitle];
UIButton *BackButton=[UIButton buttonWithType:UIButtonTypeCustom];
BackButton.Frame = CGRectMake(5, 8, 46, 30);
[BackButton setBackgroundImage:[UIImage imageNamed:@"back_btn.png"] forState:UIControlStateNormal];
[BackButton setBackgroundImage:[UIImage imageNamed:@"back_btn_selected.png"] forState:UIControlStateHighlighted];
[BackButton addTarget:self action:@selector(BackClicked) forControlEvents:UIControlEventTouchUpInside];
[TopBarImg addSubview:BackButton];
[self.view addSubview:NavigationView];
And add one method for back Button click action as follows -
-(void)BackClicked { [self.navigationController popViewControllerAnimated:YES]; }