2012-01-13 70 views
0

我做了一個iPad應用程序,在我的導航控制的標題,圖像中目標C導航欄

現在

標題欄,我希望把圖像在左邊,所以我隱藏標題欄用標籤,但標籤不覆蓋標題欄的整個寬度

IL APP在屏幕SHOT,

這裏是代碼片段,

UILabel *titleView = (UILabel *)self.navigationItem.titleView; 
    if (!titleView) { 
     titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 800, 50)]; 
     titleView.backgroundColor = [UIColor clearColor]; 
     titleView.font = [UIFont boldSystemFontOfSize:20.0]; 
     titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; 

     titleView.textColor = [UIColor yellowColor]; // Change to desired color 


     titleView.backgroundColor=[UIColor blackColor]; 

     titleView.text = @"IL APP"; 
     titleView.textAlignment=UITextAlignmentCenter; 
     //self.navigationItem.titleView = titleView; 
     [self.navigationItem setTitleView:titleView]; 
     [titleView release]; 
    } 

screen

+0

我不明白你的預期結果是什麼。你能解釋一下嗎? – 2012-01-13 06:05:23

+0

在標題欄中,我用黃色書寫了IL APP,我想將圖像放在那裏,在左側.... – GAMA 2012-01-13 06:08:43

+0

您必須在導航欄的左側顯示標籤? – Hiren 2012-01-13 06:14:05

回答

3

@Gama至於你的問題而言,你是要求把一個形象,但被描述你真正的問題是,標籤不包含標題欄。爲了覆蓋正常,你可以使用

UILabel *titleView = (UILabel *)self.navigationItem.titleView; 
if (!titleView) { 
    titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 32)]; 
    titleView.backgroundColor = [UIColor clearColor]; 
    titleView.font = [UIFont boldSystemFontOfSize:20.0]; 
    titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5]; 
    titleView.textColor = [UIColor yellowColor]; // Change to desired color 
    //titleView.backgroundColor=[UIColor blackColor]; 
    titleView.text = @"IL APP"; 
    titleView.textAlignment=UITextAlignmentCenter; 
    [self.navigationItem setTitleView:titleView]; 
    [titleView release]; 
} 
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]]; 

對於使用圖像您可以使用圖像視圖,而不是一個標籤被指定爲titleview的爲navigationItem。

希望它可以幫助

+0

老兄,它工作正常,你搖滾,現在我想把圖像放在我的導航欄中,我應該怎麼做? – GAMA 2012-01-13 06:28:24

+0

你要在哪裏放置圖像,創建一個具有所需圖像的UIImageView並將其分配給titleView,就像指定了標籤一樣。如果你想使用自定義圖像的左或右欄按鈕,那麼你可以使用UIBarButton的initWithImage:方法 – 2012-01-13 09:56:54

+0

我能夠把圖像,但不幸它總是對齊在中心,而我想它從左側開始和應該穿過標題欄。我創建了標題欄大小的圖像(縱向視圖),但是當應用程序加載時,它會從左右角落留下一些空間。同樣在橫向視圖中,空間更寬(因爲圖像尺寸來自肖像)。我可以根據方向加載適當的圖像,但想知道是否有更好的方法。 – GAMA 2012-01-13 11:08:56

0

我試圖解決你的問題,但我得到了同樣的結果。

但是你可以隱藏導航欄,並添加在UILabelViewController

self.navigationController.navigationBarHidden = FALSE; 
[self.view addSubview:titleview]; 

UIImageView *imageNav = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @"navImage.png"]]; 
[self.navigationController.navigationBar addSubview:imagenav]; 

[self.navigationController.navigationBar sendSubviewToBack:imageNav]; 
[imageNav release]; 
+0

我試過了,self.navigationController.navigationBarHidden = TRUE;導航欄消失了,但是在我的新頁面裏沒有後退按鈕,回去後,我想要返回按鈕。 – GAMA 2012-01-13 06:16:13

+0

在titleview中添加一個按鈕看起來像後退按鈕,並把按鈕點擊事件的彈出方法 – Hiren 2012-01-13 06:18:35

+0

我試圖在我的新導航裏面我的負載方法,self.navigationController.navigationBarHidden = FALSE ;,我得到的按鈕,但是當我點擊後退按鈕,它來到主頁,但與導航欄 – GAMA 2012-01-13 06:22:07

0

嗨,我想做同樣的事情,我看到你的帖子,我基本上使用了一些您所提供的代碼,並修改了一些,得到了它的工作見下文:

//declare and initialize your imageView 
UIImageView *titleImage = (UIImageView *)self.navigationItem.titleView; 

titleImage = [[UIImageView alloc] 
initWithFrame:CGRectMake((self.navigationController.navigationBar.frame.size.width/2) 
- (100/2), 0, 100, self.navigationController.navigationBar.frame.size.height)]; 

//setting the image for UIImageView 
titleImage.image = [UIImage imageName:@"photo.jpg"]; 

//注意:你必須在底部做這一行才能將圖像添加到導航欄試試看!

self.navigationItem.titleView = titleImage;