2012-11-25 32 views
0

我在view view控制器中有viewWillAppear中的以下代碼。在導航欄中清除背景圖像

- (void) viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

    UIImage *image = [UIImage imageNamed:@"navbar.png"]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
    [self.navigationController.navigationBar addSubview:imageView]; 
} 

如何清除導航欄背景圖片,已經有添加新的導航欄圖像在上面的代碼之前?

另外,如何爲每個不同的控制器設置不同的導航欄背景圖像是最有效的方式?謝謝!

+0

使用UIAppearance設置不同的導航欄的樣式。 看到http://stackoverflow.com/questions/8736171/how-to-customize-uinavigationbar-in-ios5?lq=1 –

回答

1

在您的viewWillAppear方法中,使用以下調用來設置導航欄的背景圖像。

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

如果您的應用支持的景觀,以及,你還需要設置背景圖片的高度不同的景觀導航欄:

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar_landscape.png"] forBarMetrics:UIBarMetricsLandscapePhone]; 
+0

我有一個問題 - 我們如何防止後退按鈕從人的時候被動畫點擊返回?點擊後面的按鈕就會滑到中心位置。我們怎麼能阻止呢? –

+0

在viewWillDisappear方法中,將後退按鈕設置爲nil:self.navigationItem.leftBarButtonItem = nil – meim