31

Tôi muốn một hình ảnh chiếm toàn bộ thanh điều hướng. Đây là điều hướng đi kèm với ứng dụng dựa trên điều hướng. Nó xuất hiện trên RootViewController với UITableView đi kèm. Tôi đã nhìn thấy một số ví dụ về cách thức này có thể làm việc.Thêm hình ảnh vào thanh điều hướng

Set thanh điều hướng tiêu đề:

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

Vấn đề có nó chỉ bao gồm các danh hiệu chứ không phải là toàn bộ thanh điều hướng.

Ngoài ra còn có chủ đề này: http://discussions.apple.com/message.jspa?messageID=9254241#9254241. Về phía cuối, giải pháp sẽ sử dụng thanh tab mà tôi không sử dụng. Đó là phức tạp để thiết lập một nền thanh điều hướng? Có kỹ thuật đơn giản nào khác không?

Tôi muốn có nền tảng cho điều hướng và vẫn có thể sử dụng văn bản tiêu đề.

Trả lời

36

Trong trường hợp của bạn, this solution found in another answer sẽ hoạt động tốt.

Với "CustomImage" loại thêm vào UINavigationBar, bạn có thể sau đó chỉ cần gọi:

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

Mã này nên đi trong phương pháp

- (void)viewWillAppear:(BOOL)animated 

của bộ điều khiển xem nơi bạn muốn có hình ảnh tùy chỉnh. Và, trong trường hợp đó bạn nên gọi tốt hơn:

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

trước setBackgroundImage (nếu không nó sẽ được bổ sung nhiều lần ...)

+0

Hoàn hảo. Cảm ơn. Ngoài ra, bạn có biết làm thế nào tôi có thể thiết lập màu sắc thanh tiêu đề? Tôi đã thử điều này nhưng nó không hoạt động: [self.navigationController.navigationBar.titleView setBackgroundColor: [UIColor blueColor]]; – 4thSpace

+0

Tôi cũng đã thử [navBar setBackgroundColor: [UIColor blueColor]]; – 4thSpace

+0

Có thuộc tính tintColor: thử navBar.tintColor = [UIColor blueColor]; –

8

Có thực sự là một cách dễ dàng hơn nhiều để thêm một hình nền cho bất kỳ UIView lớp hoặc phân lớp. Nó không yêu cầu phân loại hoặc mở rộng lớp (subclassing), và bạn có thể làm điều này trên cơ sở "khi cần thiết". Ví dụ, để thêm một hình ảnh nền cho thanh điều hướng một cái nhìn khiển của, làm như sau:

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

Bạn sẽ cần phải nhớ để thêm khung Quartz Core để dự án của bạn và thêm #import <QuartzCore/QuartzCore.h> bất cứ nơi nào bạn cần phải làm điều này . Đây là một cách sạch hơn, đơn giản hơn để thay đổi lớp bản vẽ của bất kỳ thứ gì thừa kế từ UIView. Tất nhiên, nếu bạn muốn thực hiện một hiệu ứng tương tự cho tất cả các thanh điều hướng hoặc thanh tab, thì phân lớp phụ có ý nghĩa.

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

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

tôi đã sử dụng cmp giải pháp 's và thêm một số logic để xoá bỏ nó như tôi chỉ muốn có một hình nền tùy chỉnh trên màn hình chủ trong vòng trên quan điểm xuất hiện.

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

Tôi cũng cố gắng sử dụng các phương pháp clearBackgroundImage gợi ý nhưng không thể làm cho nó làm việc vì vậy tôi đã đưa ra hình ảnh một thẻ và sau đó loại bỏ nó trong viewcontrollers khác vào xem sẽ xuất hiện .

OtherViewController.m

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

của nó thay đổi cho iOS6, để làm cho nó làm việc trong ios 6 sử dụng:

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

Chỉ cần thêm dòng này.

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

Bam! Một dòng và thực hiện.

+2

lol jk này b một trong những câu trả lời tốt nhất da tôi eva thấy trong cuộc sống ma! – GangstaGraham

0

chỉ cần đi bộ điều khiển xem và dán vào siêu viewDidLoad và thay thế hình ảnh của bạn trong mainlogo và sau đó đặt tiêu đề chuyển hướng trong thiết lập của bạn image logo

- (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

Làm cách nào để cải thiện các câu trả lời khác ở đây? – Mark

+0

@yogesh Bạn biết câu trả lời, nhưng tôi nghĩ rằng mã trông khá đẹp với một số nhận xét/bước. –

+0

@Mark, bạn có thể thêm điều này vào chế độ xem siêu tải và –

0

Thêm mã trong appdelegate đã kết thúc với phương pháp

tung ra
#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]; 
} 
Các vấn đề liên quan