2012-04-08 81 views
1

我躲在我的導航使用:隱藏導航欄,但沒有後退按鈕

[self.navigationController setNavigationBarHidden:YES animated:YES]; 

但我需要不隱藏後退按鈕,這是可能的?

+0

也許不..因爲後退按鈕是導航欄的一部分,恐怕沒有。 – 2012-04-08 17:40:15

回答

3

nevan king是正確的但是您可以簡單地更改導航欄的背景圖像或將其設置爲零。如果您將其設置爲零或提供透明的BG圖像,您將獲得所需的效果。

對於iOS> = 5.0,你可以簡單地設置外觀:

if([navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) // needed if iOS older than 5.0 is also supported 
    [navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault]; 

你可以做任何你有一個指針到您的導航欄。例如。在您的ViewControllerviewDidLoad方法的內部。

對於老版本的iOS你以一個UINavigationBar需要一個解決辦法,並覆蓋drawRect方法:如果你想支持所有iOS版本

@implementation UINavigationBar (BackgroundImage) 
- (void)drawRect:(CGRect)rect { 
    UIImage *image = [UIImage imageNamed: @""]; 
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
} 
@end 

兩種方法都兼容。
因此,您應該記住,後退按鈕使用相同的背景圖像。所以你需要定製一個。

UIImage *bgImageNormal = [UIImage imageNamed:@"backButtonImage.png"]; 

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button setBackgroundImage: bgImageNormal forState:UIControlStateNormal]; 

button.frame= CGRectMake(0.0, 0.0, bgImageNormal.size.width, bgImageNormal.size.height); 
[button addTarget:self action:@selector(navigationBarBackButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside]; // your action method here 

UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithCustomView:button]; 
self.navigationItem.leftBarButtonItem = closeButton; 
[closeButton release]; 

這段代碼需要爲您推送到導航欄的每個ViewController實現。它的好地方也在viewDidLoad方法中。

+0

這是我的解決方案,第一個...在哪裏把代碼?在視圖中加載? – Acunamatata 2012-04-09 17:59:14

+0

我已經用推薦的代碼更新了答案。 iOS 5方法和自定義後退按鈕可以在任何地方訪問navBar或您推送給它的viewController。 iOS 5之前的解決方法需要在自己的UINavigationBar類中實現。 – yinkou 2012-04-09 18:13:07

0

後退按鈕由導航欄創建並始終是導航欄的一部分,因此無法完成。您可以在用戶觸摸屏幕時隱藏並重新顯示導航欄(這是您查看單張照片時照片應用的功能),也可以創建按鈕並將其永久保存在屏幕的左上角。您還可以使導航欄部分透明,以便顯示下面的內容。

+0

我想清除導航欄下的垃圾陰影......這是可能的 – Acunamatata 2012-04-08 17:26:48

+0

哪個影子?我沒有得到.. – 2012-04-08 17:41:08

+0

如果您使用的是iOS 5,則可以自定義導航欄的外觀。查看WWDC Session 114視頻或iOS 5文檔中的「UIKit控件的自定義外觀」:https://developer.apple.com/library/ios/#releasenotes/General/WhatsNewIniPhoneOS/Articles/iOS5.html#//apple_ref/doc/uid/TP30915195-SW1 – 2012-04-08 17:43:21