UIToolBar 上面添加任意控件

转载自:  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>去


2.在UIToolBar 上 添加  UILabel

    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>文本

3.在UIToolBar 上 添加  滑动条 UIProgressView


    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;
	}
}

5.在UIToolBar上 添加 Title


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];  

6. 在UIToolBar上添加image


[myToolBarItems addObject:[[[UIBarButtonItem alloc]  
                initWithImage:[UIImage imageNamed:@"myImage.png"]   
                        style:UIBarButtonItemStylePlain   
                       target:self   
                       action:@selector(action)]];  

7. 在UIToolBar上添加SystemItem


[myToolBarItems addObject:[[[UIBarButtonItem alloc]  
            initWithBarButtonSystemItem:UIBarButtonSystemItemPlay   
                                 target:self   
                                 action:@selector(action)] autorelease]];  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值