[英]Hide Navigation Bar but not the back button
我使用以下方式隱藏我的導航:
[self.navigationController setNavigationBarHidden:YES animated:YES];
但我不需要隱藏后退按鈕,這可能嗎?
nevan king 是對的,但您可以簡單地更改導航欄的背景圖像或將其設置為 nil。 如果將它設置為 nil 或提供透明的 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];
只要有指向導航欄的指針,您就可以這樣做。 例如,在ViewController
的viewDidLoad
方法中。
對於較舊的 iOS 版本,您需要通過創建UINavigationBar
類別並覆蓋drawRect
方法來解決問題:
@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
如果你想支持所有 iOS 版本,這兩種方法都是兼容的。
因此,您應該記住,后退按鈕使用相同的背景圖像。 所以你需要定制一個。
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
方法中。
后退按鈕由導航欄創建並且始終是其中的一部分,因此這是不可能的。 您可以在用戶觸摸屏幕時隱藏並重新顯示導航欄(這是照片應用程序在您查看單張照片時所做的)或創建一個按鈕並將其永久放置在屏幕的左上角。 您還可以使導航欄部分透明,以便顯示下面的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.