2010-05-10 141 views
0

我已經把我的導航欄的背景,這個代碼在我的應用程序委託:UINavigationBar的背景圖像問題

@implementation UINavigationBar (UINavigationBarCategory) 
- (void)drawRect:(CGRect)rect { 
    UIImage *backgroundImage = [UIImage imageNamed: @"Nav-rightpane.png"]; 

    CGContextDrawImage(UIGraphicsGetCurrentContext(), 
         CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), 
         backgroundImage.CGImage); 
} 
@end 

這完美的作品,但現在我必須改變的背景,在UIPopoverController默認。任何想法呢?

謝謝

回答

0

請勿使用此解決方案。只需刪除您的UINavigationBar類別並實施您自己的UINavigationController即可。

@interface XNavigationController : UINavigationController{} 

,並在實施變革重寫viewDidLoad方法,當你想改變導航欄的背景

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIImage *img = [UIImage imageNamed: @"Nav-rightpane.png"]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:img]; 
    [imageView setFrame:self.navigationBar.bounds]; 
    [imageView setContentMode:UIViewContentModeScaleToFill]; 
    [self.navigationBar addSubview:imageView]; 
    [imageView release]; 
} 

所以用這個navigationController

0

預iOS5中,所推薦的蘋果工程師的正確的解決方案是使用類別和使用方法交叉混合。這被證實會在iOS 5上突破,這個版本非常接近發佈。

相反,您應該創建一個UINavigationBar的子類,覆蓋drawRect:方法,然後使用Interface Builder來定義您的導航控制器。界面生成器可以讓你更改導航欄的類名來定製子類:

enter image description here

enter image description here

如果你想避免界面生成器,你可以創建一個臨時XIB和加載的一個實例從它的導航控制器,然後使用NSKeyedArchiver將其歸檔到一個文件。然後在命令行上使用xxd -i <filename>爲導航控制器的原始字節生成C代碼。然後將其粘貼到源文件中,並在需要實例時將其解壓縮。

這似乎很多工作,但在論壇上的WWDC視頻和工程師一再表示,這是唯一的安全可靠的方式來做你想做的。

在iOS 5中,這一切變得非常簡單。