使用UINavigationController默认的自定义背景UIToolbar
问题描述:
我想为我的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,所以这不起作用?!?感谢
答
要创建自定义的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];
希望这会有所帮助。
答
我也发现你必须设置你的UIToolbar的背景清除:
[toolbar setBackgroundColor:[UIColor clearColor]];
答
在您的自定义的UINavigationController的viewDidLoad使用下面的代码:
[self.toolbar setBackgroundImage:[UIImage imageNamed:@"mytoolbarbg.png"]
forToolbarPosition:1
barMetrics:UIBarMetricsDefault];