在iPhone中设置导航栏的标题

时间:2013-06-27 11:11:00

标签: iphone ipad uinavigationbar title

我正在向导航栏添加图片,但是当我想添加标题

时,它可以正常工作
   self.title=@"Activity";

它没有显示任何内容

使用另一种方式我也添加标题标签,但这适用于一个视图控制器,但在第二个控制器我更改标题,但它也显示prevoius标题

这是我的代码

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

3 个答案:

答案 0 :(得分:2)

不是在UINavigationBar中添加图像,而是使用波纹管代码设置背景图像..

UINavigationBar *navBar = [[self navigationController] navigationBar];
UIImage *backgroundImage = [UIImage imageNamed:@"Nav.png"];
[navBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];

然后设置标题如下...

self.title=@"Activity";

<强>更新

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

答案 1 :(得分:0)

如果采用另一种方式将子视图添加到导航栏,则在viewWillDisAppear方法视图中,您应该将标题设置为隐藏,并在viewWillAppear方法设置隐藏false.i面临与您相同的问题。

因此,只要您离开当前视图viewWillDisAppear方法,它就会隐藏当前视图的标题,并且当您再次在同一视图中时,viewWillAppear方法将再次显示标题。

答案 2 :(得分:0)

您只需隐藏默认导航栏即可。并添加以下代码 -

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

为后退按钮点击操作添加一个方法,如下所示 -

- (无效)BackClicked {     [self.navigationController popViewControllerAnimated:YES]; }