2012-07-09 74 views
1

我有我想創建一個自定義uinavigationn吧我想這碼iphone:在導航欄設置背景圖片是不恰當

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height)]; 
//here for v, width= navBar width and height=navBar height 
//  
[view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"newsBanner.png"]]]; 
[self.navigationController.navigationBar addSubview:view]; 
表視圖

的問題是圖像不居中導致其大小爲640 x 72 ...有沒有辦法讓它合適?

回答

2

您的導航欄應該只有44像素高。最重要的是,它的最大寬度是320點(在視網膜設備上是640像素,在非高清設備上是320像素)。所以你的圖片尺寸太大了。作出這樣的具有320x44尺寸的圖像,然後做到這一點:

UINavigationBar *theBar = self.navigationController.navigationBar; 
if ([theBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) { 
    UIImage *image = [UIImage imageNamed:@"your image here"]; 
    [theBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; 
} 

當然,這隻會在iOS 5的工作,你需要找到處理設置在操作系統後臺低於5.0等方式進行。但是有很多堆棧溢出問題可以解決這個問題:)

+0

作爲一種替代方案,您可能可以在代碼中縮放圖像,但這並不是一件有趣的事情,可能比僅僅將圖像設置爲正確的尺寸更麻煩。 – bgoers 2012-07-09 20:07:08

+0

當您嚮導航項添加提示時,圖像會重複出現,並且我們無法將圖像拉伸至導航欄,因此存在問題。 – djleop 2012-12-05 13:51:19