Setting title for UINavigation bar in iphone

限于喜欢 提交于 2019-12-02 04:23:50

问题


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

using another way i add title label also but this works fine for one view controller but on second controller i change title but it also shows prevoius title

here is my code

UIImage *image = [UIImage imageNamed:@"Nav.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];




[self.navigationController.navigationBar addSubview:imageView]; 


   titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(50,2,250,36)];
     titleLabel.text=@"Activity";
titleLabel.textColor=[UIColor whiteColor];
titleLabel.backgroundColor=[UIColor clearColor];
titleLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size :18];



//titleLabel boldSystemFontOfSize:14.0;
[self.navigationController.navigationBar addSubview:titleLabel];

回答1:


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";



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/17341575/setting-title-for-uinavigation-bar-in-iphone

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