在UIToolbar中添加title

在iOS开发中,有时候会遇到需要在UIToolbar上添加Title的情况,类似于UINavigationController 的title。
下面的代码会使用UIBarButtonItem alloc 的 initWithCustomView方法来添加一个UILabel作为Title的载体。

 

UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
	UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f, 120, 21.0f)];
	[titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
	[titleLabel setBackgroundColor:[UIColor clearColor]];
	[titleLabel setTextColor:[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]];
	[titleLabel setText:@"bookmark"];
	[titleLabel setTextAlignment:UITextAlignmentCenter];
	
	
	UIBarButtonItem *titleButtonItem = [[UIBarButtonItem alloc] initWithCustomView:titleLabel];
	[titleLabel release];
	UIToolbar * toolBar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
	NSArray *toolArray = [NSArray arrayWithObjects:spaceItem,titleButtonItem,spaceItem,nil];
	[toolBar setItems:toolArray ];
	

This entry was posted in ArcGIS API for iOS, iOS, iPad, iPhone4, Mac OS X.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">