2009-10-12 175 views
31

我想讓圖像佔據所有導航欄。這是基於導航的應用程序附帶的導航。它出現在隨附的UITableView的RootViewController上。我已經看到了一些如何工作的例子。將圖像添加到導航欄

集導航欄標題:

UIImage *image = [UIImage imageNamed:@"TableviewCellLightBlue.png"]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
[self.navigationController.navigationBar.topItem setTitleView:imageView]; 

的問題有它僅覆蓋了標題,而不是整個導航欄。

還有這個線程:http://discussions.apple.com/message.jspa?messageID=9254241#9254241。最後,該解決方案看起來使用一個標籤欄,我沒有使用。設置導航欄背景很複雜?還有其他一些更簡單的技術嗎?

我想有一個導航的背景,仍然可以使用標題文本。

回答

36

在你的情況下,this solution found in another answer會運作良好。

與添加到UINavigationBar的了「CustomImage」類別, 然後你可以只要致電:

UINavigationBar *navBar = self.navigationController.navigationBar; 
UIImage *image = [UIImage imageNamed:@"yourNavBarBackground.png"]; 
[navBar setBackgroundImage:image]; 

此代碼應該在方法

- (void)viewWillAppear:(BOOL)animated 
視圖控制器的要

有自定義圖像。 而且,在這種情況下,你應該更好地叫:

[navBar clearBackgroundImage]; // Clear any previously added background image 

前了setBackgroundImage(否則會被多次添加...)

+0

完美。謝謝。另外,你知道我可以如何設置標題欄的顏色嗎?我試過這個,但它不工作:[self.navigationController.navigationBar.titleView setBackgroundColor:[UIColor blueColor]]; – 4thSpace 2009-10-12 14:26:51

+0

我也試過[navBar setBackgroundColor:[UIColor blueColor]]; – 4thSpace 2009-10-12 14:28:38

+0

有tintColor屬性:嘗試navBar.tintColor = [UIColor blueColor]; – 2009-10-12 15:19:47

8

其實是有背景圖像添加到任何一個更爲簡單的方法UIView類或子類。它不需要類別分類或擴展(子類別),您可以在「根據需要」的基礎上進行此操作。例如,背景圖像添加到視圖控制器的導航欄,做到以下幾點:

self.navigationController.navigationBar.layer.contents = (id)[UIImage 
    imageNamed:@"background.png"].CGImage; 

你需要記住的石英核心框架添加到您的項目,並添加#import <QuartzCore/QuartzCore.h>無論你需要這樣做。這是一種更簡潔,更簡單的方法,用於更改從UIView繼承的任何東西的繪圖層。當然,如果你想爲所有導航欄或標籤欄實現類似的效果,那麼子類化是有道理的。

9
UIImage *image = [UIImage imageNamed:@"YourImage.png"]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

[self.navigationController.navigationBar addSubview:imageView]; 
3

使用CMP的溶液並添加了一些邏輯以除去它,因爲我只想要內上視圖顯示在主屏幕上的自定義的背景圖片。

HomeViewController.m

UIImage *image = [UIImage imageNamed:@"HomeTitleBG.png"]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
imageView.tag = 10; 

UIImageView *testImgView = (UIImageView *)[self.navigationController.navigationBar viewWithTag:10]; 

if (testImgView != nil) 
{ 
    NSLog(@"%s yes there is a bg image so remove it then add it so it doesn't double it", __FUNCTION__); 
    [testImgView removeFromSuperview]; 

} else { 
    NSLog(@"%s no there isn't a bg image so add it ", __FUNCTION__); 
} 

[self.navigationController.navigationBar addSubview:imageView]; 


[imageView release]; 

我也試着使用建議clearBackgroundImage方法,但不能讓它的工作,所以我給了圖像的標籤,然後在視圖中的其他viewcontrollers刪除它會出現。

OtherViewController。米

UIImageView *testImgView = (UIImageView *)[self.navigationController.navigationBar viewWithTag:10]; 

if (testImgView != nil) 
{ 
    NSLog(@"%s yes there is a bg image so remove it", __FUNCTION__); 
    [testImgView removeFromSuperview]; 
} 

`

4
UIImage *logo = [UIImage imageNamed:@"my_logo"]; 
UIImageView *logoView = [[UIImageView alloc]initWithImage:logo]; 
logoView.frame = CGRectMake(0, 0, 320, 37); 

UINavigationController *searchNavCtrl = [[UINavigationController alloc] initWithRootViewController:searchViewController]; 
searchNavCtrl.navigationBar.barStyle = UIBarStyleBlack; 


//searchNavCtrl.navigationItem.titleView = logoView; 
//[searchNavCtrl.navigationController.navigationBar.topItem setTitleView:logoView]; 
[searchNavCtrl.navigationBar addSubview:logoView]; 

[logoView release]; 
23

它改變了iOS6的,使它在運行iOS 6中使用:

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

只需添加這條線。

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar.png"] forBarMetrics:UIBarMetricsDefault]; 

Bam!一行完成。

+2

哈哈jk這是我最好的回答之一我在馬的生活中看到的伊娃! – GangstaGraham 2013-08-23 10:31:00

0

剛去的視圖控制器和超級viewDidLoad中 粘貼在mainlogo更換你的圖像,然後設置導航標題設置你的形象標誌

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    //set your image frame 
    UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,70,45)] ; 

    //set your image logo replace to the main-logo 
    [image setImage:[UIImage imageNamed:@"main-logo"]]; 

    [self.navigationController.navigationBar.topItem setTitleView:image]; 

} 
+2

這對其他答案的改進如何? – Mark 2014-05-13 13:43:44

+0

@yogesh你知道答案,但我認爲代碼看起來很漂亮,有一些評論/步驟。 – 2014-05-13 13:55:11

+0

@Mark你可以添加這個超級視圖didload和 – 2014-05-15 05:37:20

0

中的appdelegate添加代碼沒有完成與發射方法

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0] 

if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) 
{ 
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation_or.png"] forBarMetrics:UIBarMetricsDefault]; 
    [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault]; 
} 
else 
{ 
    [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)]; 

    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; 

    // Uncomment to assign a custom backgroung image 
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigation_or.png"] forBarMetrics:UIBarMetricsDefault]; 

    // Uncomment to change the back indicator image 
    [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]]; 

    [[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]]; 
    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@""]]; 

    // Uncomment to change the font style of the title 

    NSShadow *shadow = [[NSShadow alloc] init]; 
    shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8]; 
    shadow.shadowOffset = CGSizeMake(0, 0); 

    [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,shadow, NSShadowAttributeName,[UIFont fontWithName:@"HelveticaNeue-Bold" size:17], NSFontAttributeName, nil]]; 

    [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:0.0 forBarMetrics:UIBarMetricsDefault]; 
}