Setting title for UINavigation bar in iphone

前端 未结 3 1283
滥情空心
滥情空心 2021-01-22 05:09

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

相关标签:
3条回答
  • 2021-01-22 05:54

    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";
    
    0 讨论(0)
  • 2021-01-22 06:00

    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.

    0 讨论(0)
  • 2021-01-22 06:02

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

    0 讨论(0)
提交回复
热议问题