我有一個問題,要更改IOS版本的UINavigationBar
的背景圖像< 5.我已經讀過一個很好的解決方案,它基於方法調整,但這個問題解決方案是當我添加它涵蓋的圖像時,所有內容都包括導航欄上的按鈕。 我發現這部分的工作對我來說是在下面的代碼基礎的解決方案:在iOS中更改UINavigationBar的背景圖像<5
@interface UINavigationBar (UINavigationBarCategory)
-(void)setBackgroundImage:(UIImage*)image withTag:(NSInteger)bgTag;
-(void)resetBackground:(NSInteger)bgTag;
@end
@implementation UINavigationBar (UINavigationBarCategory)
-(void)setBackgroundImage:(UIImage*)image withTag:(NSInteger)bgTag{
if(image == NULL){ //might be called with NULL argument
return;
}
UIImageView *aTabBarBackground = [[UIImageView alloc]initWithImage:image];
aTabBarBackground.frame = CGRectMake(0,0,self.frame.size.width,self.frame.size.height);
aTabBarBackground.tag = bgTag;
[self addSubview:aTabBarBackground];
//[self sendSubviewToBack:aTabBarBackground];
[aTabBarBackground release];
}
-(void)setRightButton:(UIButton*)button withTag:(NSInteger)bgTag{
if(button == NULL){ //might be called with NULL argument
return;
}
[self addSubview:button];
}
/* input: The tag you chose to identify the view */
-(void)resetBackground:(NSInteger)bgTag {
[self sendSubviewToBack:[self viewWithTag:bgTag]];
}
@end
我用這個類別中我ViewWillAppear
方法是這樣的:
-(void) viewWillAppear:(BOOL)animated {
UIImage *backgroundImage = [UIImage imageNamed:@"background_confernce_import_logo"];
if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
[self.navigationController.navigationBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];
}
else{
[[self.navigationController navigationBar] setBackgroundImage:backgroundImage withTag:8675309];
}
}
在else子句我打電話setBackgroundImage
。沒問題,但問題是,如果我在頁面1的導航欄上有一個右鍵,並在回到第1頁後轉到第2頁,則該按鈕將消失。我應該在我的應用程序的每個頁面中更改導航欄的背景圖像,如viewWillAppear
方法中放置新圖像的方法。 任何幫助將不勝感激。在IOS 5下不存在這樣的問題,但它應該可以在兩個版本上運行。
[自定義背景UINavigationBar的(http://stackoverflow.com/questions/704558/custom-uinavigationbar-background) – Till
@Till的可能重複,答案可能是幾乎相同(爲紐帶您發佈)但問題稍有不同,因爲它詢問了實施自定義背景的具體嘗試...... – sergio
@sergio公平不夠 - 有道理。 – Till