2013-06-27 53 views
2

我將圖片添加到導航欄它工作正常,但當我想添加標題設置標題爲iphone

[email protected]"Activity"; 

它採用另一種方式我添加標題標籤不顯示任何

UINavigation酒吧也不過這個工作得很好一個視圖控制器,但是,第二控制器我改變標題,但它也表明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)]; 
    [email protected]"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]; 

回答

2

而不是使用波紋管代碼UINavigationBar添加圖片,設置背景圖片..

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

,然後設置標題像波紋管......

[email protected]"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]; 
     } 
    } 
    [email protected]"Activity"; 
+0

它給錯誤UIBarMetricsDefault –

+0

你得到哪些錯誤? –

+0

@JawAli現在使用我更新的代碼... –

0

如果以另一種方式添加子導航欄,然後在viewWillDisAppear視圖的方法你應該設置標題隱藏和viewWillAppear方法設置隱藏false.i面臨同樣的問題,你的。

因此,只要你離開你當前的視圖,viewWillDisAppear方法就會調用,它會隱藏當前視圖的標題,當你再次在同一視圖中時,viewWillAppear方法將再次顯示標題。

+0

好,謝謝讓我試試 –

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