当前位置: 代码迷 >> 综合 >> UIToolBar 上面添加任意控件
  详细解决方案

UIToolBar 上面添加任意控件

热度:65   发布时间:2024-01-11 22:41:29.0

转自:http://blog.csdn.net/light_jewel/article/details/7768045

一.  在UIToolBar 上添加任意多个不同或相同的控件

1. 在UIToolBar 上添加相同的控件

[cpp]  view plain copy
  1.     self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque ;  
  2.       
  3.     //设置工具栏的背景颜色  
  4.       
  5.     NSMutableArray *buttons = [[NSMutableArray alloc]initWithCapacity:4];  
  6.       
  7.     //设置添加按钮的数量  
  8.       
  9.     UIBarButtonItem *flexibleSpaceItem;  
  10.     flexibleSpaceItem =[[[UIBarButtonItem alloc]  
  11.                          initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  
  12.                          target:self  
  13.                          action:NULL]autorelease];  
  14.     [buttons addObject:flexibleSpaceItem];  
  15.       
  16. //UIBarButtonSystemItemFixedSpace和UIBarButtonSystemItemFlexibleSpace都是系统提供的用于占位的按钮样式。  
  17. //使按钮与按钮之间有间距  
  18.       
  19.     UIBarButtonItem *Item;  
  20.     Item = [[UIBarButtonItem alloc]  
  21.             initWithImage:[UIImage imageNamed:@"down.png"]  
  22.             style:UIBarButtonItemStylePlain  
  23.             target:self  
  24.             action:@selector(decrement:)];  
  25.     [buttons addObject:Item];  
  26.       
  27.     //添加第一个图标按钮  
  28.       
  29.     Item = [[UIBarButtonItem alloc]  
  30.              initWithImage:[UIImage imageNamed:@"up.png"]  
  31.             style:UIBarButtonItemStylePlain  
  32.             target:self  
  33.             action:@selector(increment:)];  
  34.     [buttons addObject:Item];  
  35.       
  36.     //添加第二个图标按钮  
  37.       
  38.     flexibleSpaceItem = [[[UIBarButtonItem alloc]  
  39.                           initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace  
  40.                           target:self  
  41.                           action:NULL]autorelease];  
  42.     [buttons addObject:flexibleSpaceItem];  
  43.           
  44.     UIToolbar *toolBar = [[UIToolbar alloc]init];  
  45.     toolBar.barStyle = UIBarStyleBlackOpaque ;  
  46.     [toolBar setItems:buttons animated:YES];  
  47.     [toolBar sizeToFit];  
  48.     self.navigationItem.titleView = toolBar ;  
  49.       
  50.     //将这些按钮添加到toolbar上面去  


2.在UIToolBar 上添加 UILabel

[cpp]  view plain copy
  1. UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40.0f, 20.0f, 45.0f, 10.0f)];    
  2. myLabel.font=[UIFont systemFontOfSize:10];    
  3. myLabel.backgroundColor = [UIColor whiteColor];    
  4. myLabel.textAlignment=UITextAlignmentCenter;    
  5. myLabel.text  = @"aa";  
  6. UIBarButtonItem *myButtonItem = [[UIBarButtonItem alloc]initWithCustomView:myLabel];    
  7. [buttons addObject: myButtonItem];     //添加文本  

3.在UIToolBar 上添加 滑动条 UIProgressView


[cpp]  view plain copy
  1.     UIProgressView *myProgress = [[UIProgressView alloc] initWithFrame:  
  2.                                   CGRectMake(65.0f, 20.0f, 90.0f, 10.0f)];   
  3.       
  4.     UIBarButtonItem *myButton = [[UIBarButtonItem alloc]initWithCustomView:myProgress];   
  5.       
  6.     [buttons addObject: myButton];       //添加滑动条<span style="color:#0bb3fc;">  
  7. </span>  

4.在UIToolBar上添加UISegmentedControl UISegmentedControl的作用是可以让有相同功能的按钮紧凑的排列在一起


[cpp]  view plain copy
  1. NSArray *buttonNames = [NSArray arrayWithObjects:@"One", @"Two", @"Three", @"Four", nil];  
  2.   
  3. UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:buttonNames];  
  4.   
  5. segmentedControl.momentary = YES;   
  6.   
  7. //如果它被设置为YES,选定时亮 点击过后不亮  如果注释掉选中的项一直被显示高亮   
  8.   
  9. [(UITextView *)self.view setText:@""];  
  10.   
  11. segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;//若出界的话则自动扩展其宽度  
  12.   
  13. segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;  
  14.   
  15. segmentedControl.frame = CGRectMake(0, 0, 400, 30);  
  16.   
  17. [segmentedControl addTarget:self action:@selector(segmentAction:)  
  18.    
  19.                        forControlEvents:UIControlEventValueChanged];  
  20.   
  21. //self.navigationItem.titleView = segmentedControl;  
  22.   
  23. [self.navigationController.navigationBar addSubview: segmentedControl]; //与上面一句话同样的效果  

[cpp]  view plain copy
  1. -(void) segmentAction: (id) sender  
  2. {  
  3.     switch([sender selectedSegmentIndex] + 1)  
  4.     {  
  5.         case 1: [(UITextView *) self.view setText:@"\n\nOne"]; break;  
  6.               
  7.         case 2: [(UITextView *) self.view setText:@"\n\nTwo"]; break;  
  8.               
  9.         case 3: [(UITextView *) self.view setText:@"\n\nThree"]; break;  
  10.               
  11.         case 4: [(UITextView *) self.view setText:@"\n\nFour"]; break;    
  12.               
  13.         defaultbreak;  
  14.     }  
  15. }  

5.在UIToolBar上添加 Title


[cpp]  view plain copy
  1. UIToolbar *myToolBar = [[UIToolbar alloc] initWithFrame:    
  2.                      CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];    
  3.                                                         
  4. NSMutableArray *myToolBarItems = [NSMutableArray array];    
  5. [myToolBarItems addObject:[[[UIBarButtonItem alloc]    
  6.                               initWithTitle:@"myTile"     
  7.                                       style:UIBarButtonItemStylePlain     
  8.                                      target:self     
  9.                                      action:@selector(action)] autorelease]];    
  10. [myToolBar setItems:myToolBarItems animated:YES];    

6. 在UIToolBar上添加image


[cpp]  view plain copy
  1. [myToolBarItems addObject:[[[UIBarButtonItem alloc]    
  2.                 initWithImage:[UIImage imageNamed:@"myImage.png"]     
  3.                         style:UIBarButtonItemStylePlain     
  4.                        target:self     
  5.                        action:@selector(action)]];    

7. 在UIToolBar上添加SystemItem


[cpp]  view plain copy
  1. [myToolBarItems addObject:[[[UIBarButtonItem alloc]    
  2.             initWithBarButtonSystemItem:UIBarButtonSystemItemPlay     
  3.                                  target:self     
  4.                                  action:@selector(action)] autorelease]];