2010-09-06 77 views
2

我想爲我的UIToolbar使用自己的背景。使用UINavigationController默認的自定義背景UIToolbar

要做到這一點與UINavigationBar的我只是用這個類別與drawRect方法的重寫:

@implementation UIToolbar (CustomImage) 

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

這段代碼完全適用於該UINavigationBar的,但這並不爲UIToolbar的工作,是隱藏在UINavigationController中,並使用此行代碼:

self.navController.toolbarHidden = NO; 

任何人都可以幫我解決這個問題嗎?我確定UINavigationController使用標準的UIToolbar,所以這不起作用?!?感謝

回答

2

要創建自定義的UI元素就沒有必要再創建類別。使用IOS 5,您可以通過UIApperance協議更改任何UI元素的外觀。您可以像平常一樣更改個別用戶界面元素,但是當您創建主題應用程序時,真正的功能就來了。使用此新協議,您可以更改特定UI元素的所有實例的樣式,甚至可以更改特定設置(如UINavigationController中的UIBarButtonItem)中的UI元素的樣式。

改變所有在應用程序中UINavigationBars的外觀,你可以設置像這樣的背景下,

[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:@"myNavBar.png"] forBarMetrics:UIBarMetricsDefault]; 

你改變,這取決於元素使用的是改變UINavigationBar的標題屬性不同的屬性您可以使用,

[[UINavigationBar appearance] setTitleTextAttributes: 
    [NSDictionary dictionaryWithObjectsAndKeys: 
    [UIColor whiteColor], UITextAttributeTextColor, 
    [UIFont fontWithName:@"MarkerFelt-Thin" size:24], UITextAttributeFont, nil]]; 

,你也可以使用這個在其他UI元素,從而改變對UIToolbar的所有實例的背景,你可以調用

[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:@"myToolBar.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault]; 

希望這會有所幫助。

1

在您的自定義的UINavigationController的viewDidLoad使用下面的代碼:

[self.toolbar setBackgroundImage:[UIImage imageNamed:@"mytoolbarbg.png"] 
       forToolbarPosition:1 
       barMetrics:UIBarMetricsDefault];