Setting title for UINavigation bar in iphone

一世执手 提交于 2019-12-02 02:21:50

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]; }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!