转载自: http://www.verydemo.com/demo_c272_i13014.html
http://blog.csdn.net/ipromiseu/article/details/6080474
一. 在UIToolBar 上添加任意多个不同或相同的控件
1. 在UIToolBar 上添加相同的控件
self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque ; //设置工具栏的背景颜色 NSMutableArray *buttons = [[NSMutableArray alloc]initWithCapacity:4]; //设置<strong>添加</strong>按钮的数量 UIBarButtonItem *flexibleSpaceItem; flexibleSpaceItem =[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:NULL]autorelease]; [buttons addObject:flexibleSpaceItem]; //UIBarButtonSystemItemFixedSpace和UIBarButtonSystemItemFlexibleSpace都是系统提供的用于占位的按钮样式。 //使按钮与按钮之间有间距 UIBarButtonItem *Item; Item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"down.png"] style:UIBarButtonItemStylePlain target:self action:@selector(decrement:)]; [buttons addObject:Item]; //<strong>添加</strong>第一个图标按钮 Item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"up.png"] style:UIBarButtonItemStylePlain target:self action:@selector(increment:)]; [buttons addObject:Item]; //<strong>添加</strong>第二个图标按钮 flexibleSpaceItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:NULL]autorelease]; [buttons addObject:flexibleSpaceItem]; UIToolbar *toolBar = [[UIToolbar alloc]init]; toolBar.barStyle = UIBarStyleBlackOpaque ; [toolBar setItems:buttons animated:YES]; [toolBar sizeToFit]; self.navigationItem.titleView = toolBar ; //将这些按钮<strong>添加</strong>到toolbar<strong>上面</strong>去
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40.0f, 20.0f, 45.0f, 10.0f)]; myLabel.font=[UIFont systemFontOfSize:10]; myLabel.backgroundColor = [UIColor whiteColor]; myLabel.textAlignment=UITextAlignmentCenter; myLabel.text = @"aa"; UIBarButtonItem *myButtonItem = [[UIBarButtonItem alloc]initWithCustomView:myLabel]; [buttons addObject: myButtonItem]; //<strong>添加</strong>文本
UIProgressView *myProgress = [[UIProgressView alloc] initWithFrame: CGRectMake(65.0f, 20.0f, 90.0f, 10.0f)]; UIBarButtonItem *myButton = [[UIBarButtonItem alloc]initWithCustomView:myProgress]; [buttons addObject: myButton]; //<strong>添加</strong>滑动条
4.在UIToolBar上添加UISegmentedControl UISegmentedControl的作用是可以让有相同功能的按钮紧凑的排列在一起
NSArray *buttonNames = [NSArray arrayWithObjects:@"One", @"Two", @"Three", @"Four", nil]; UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:buttonNames]; segmentedControl.momentary = YES; //如果它被设置为YES,选定时亮 点击过后不亮 如果注释掉选中的项一直被显示高亮 [(UITextView *)self.view setText:@""]; segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;//若出界的话则自动扩展其宽度 segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; segmentedControl.frame = CGRectMake(0, 0, 400, 30); [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged]; //self.navigationItem.titleView = segmentedControl; [self.navigationController.navigationBar addSubview: segmentedControl]; //与<strong>上面</strong>一句话同样的效果
-(void) segmentAction: (id) sender { switch([sender selectedSegmentIndex] + 1) { case 1: [(UITextView *) self.view setText:@"\n\nOne"]; break; case 2: [(UITextView *) self.view setText:@"\n\nTwo"]; break; case 3: [(UITextView *) self.view setText:@"\n\nThree"]; break; case 4: [(UITextView *) self.view setText:@"\n\nFour"]; break; default: break; } }
UIToolbar *myToolBar = [[UIToolbar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)]; NSMutableArray *myToolBarItems = [NSMutableArray array]; [myToolBarItems addObject:[[[UIBarButtonItem alloc] initWithTitle:@"myTile" style:UIBarButtonItemStylePlain target:self action:@selector(action)] autorelease]]; [myToolBar setItems:myToolBarItems animated:YES];
[myToolBarItems addObject:[[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"myImage.png"] style:UIBarButtonItemStylePlain target:self action:@selector(action)]];
[myToolBarItems addObject:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(action)] autorelease]];