2

我正在将图像添加到导航栏它工作正常但是当我想添加标题时

   self.title=@"Activity";

它没有显示任何内容

使用另一种方式我也添加了标题标签,但这对于一个视图控制器来说很好,但是在第二个控制器上我改变了标题,但它也显示了以前的标题

这是我的代码

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

3 回答 3

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";
于 2013-06-27T11:27:19.777 回答
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];

并为后退按钮单击操作添加一种方法,如下所示 -

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

于 2013-06-27T11:35:54.987 回答
0

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

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

于 2013-06-27T11:22:32.903 回答