2012-12-07 36 views
0

我無法重寫UIToolbar類的繪製方法。我想用背景圖像和一些按鈕來定製工具欄。無法重新實現UIToolbar類的繪製方法

我試了這兩個在AppDelegate.m,然後也在我的viewcontroller實施文件。這裏是我的代碼:

@implementation UIToolbar (CustomImage) 

- (void)drawRect:(CGRect)rect { 
    UIImage *image = [UIImage imageNamed: @"tabbar.png"]; 
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
} 

@end 

我添加工具欄與此代碼在視圖控制器viewDidLoad方法:

UIBarButtonItem *flexiableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil]; 
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil]; 

    NSArray *items = [NSArray arrayWithObjects:item1, flexiableItem, item2, nil]; 
    self.toolbarItems = items; 

感謝您的幫助

回答

1

也許更好的在應用程序啓動中添加此代碼?

[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:@"tabbar.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault]; 
相關問題