您的位置:首页 > 产品设计 > UI/UE

iOS:UIToolBar控件的使用

2015-08-28 21:08 573 查看
UIToolBar控件:是经常使用的一个工具条控件,虽然在上面可以添加子控件,但是toolbar中只能添加UIBarButtonItem类型的子控件,其他子控件会被包装成这种类型的,例如UIButton。通过工具栏可以用来对视图View中内容进行操作。

原理:

可以在toolBar上添加任何子控件。其实它的原理是把你要添加的子控件先加到toolbarItems数组里面,最后再把toolbarItems数组一次性放到toolbar工具栏里面。

虽然可以在toolbar中添加其他任何的视图控件如UILabel、UITextField、UIImageView等等,但是在xib/storyboard图形界面设计时,不能它们直接放置到UIToolBar中。若强行将它们拖曳到UIToolBar,会使它们放置在上层容器中,而不是UIToolBar中。所以前提是先必须添加一个视图UIView控件到toolbar中,它会被包装成UIBarButtonItem类型,然后再在UIView中添加需要的子视图控件。

举例如下:将UILabel加入到toolbar工具栏中,步骤如下:

1. 将UIView拖曳到UIToolBar(UIToolBar中自动增加了一个UIBarButtonItem,其中便是刚才插入的UIView);

2. 将UILabel(或其他控件)拖曳到刚才的UIView中;

3. 将刚才的UIView的Background设为某种颜色(如蓝色);

4. 将刚才的UIView的Background设为Default。

对toolbar进行初始化:

-initWithTitle(添加button用这个)

-initWithImage

-initWithBarButtonSystemItem(添加系统自定义的button,形状跟大小都已经固定了)下面链接里面有按钮图片样式

-initWithCustomView(添加除了button以外的View)

一、采用系统默认.xib文件中的UIToolBar,制作的工具栏(删除和添加图片)

(1)默认的View视图布局





  代码如下:需要在代码中为添加的控件人为设置frame具体坐标x,y、大小width,height

#import "ViewController.h"
#define CONTACE_VIEW_HEIGHT 50
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIToolbar *toolBar;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *barButtonitemDelete;

@end

@implementation ViewController
- (IBAction)addContact:(UIBarButtonItem *)sender
{

//让删除按钮有效
[self.barButtonitemDelete setEnabled:YES];

//在subView中已经有了3个控件
NSInteger count = self.view.subviews.count - 3;
CGRect lastFrame = self.toolBar.frame;

UIView *contactView = [[UIView alloc]init];
CGFloat gapY = 5;
CGFloat x = 0;
CGFloat y = lastFrame.origin.y + lastFrame.size.height + (CONTACE_VIEW_HEIGHT+gapY) * count;
CGFloat w = self.view.frame.size.width;

contactView.frame = CGRectMake(x,y,w,CONTACE_VIEW_HEIGHT);

//添加头像
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",arc4random_uniform(9)]];
UIImageView *face = [[UIImageView alloc]initWithImage:image];
face.frame = CGRectMake(10, 0,50,50);
[contactView addSubview:face];

//添加姓名
UILabel *labelName = [[UILabel alloc]init];
labelName.text = [NSString stringWithFormat:@"name%d",arc4random_uniform(10)];
labelName.frame = CGRectMake(10+image.size.width+50, 0, 100, 50);
[contactView addSubview:labelName];

contactView.backgroundColor = [UIColor lightGrayColor];

[self.view addSubview:contactView];
}
- (IBAction)deleteContact:(UIBarButtonItem *)sender
{
//删除视图
UIView *lastView = [self.view.subviews lastObject];
[lastView removeFromSuperview];

//如果没有了contactView,设置删除按钮无效
if(self.view.subviews.count == 3)
{
[self.barButtonitemDelete setEnabled:NO];
}
}

- (void)viewDidLoad {
[super viewDidLoad];
//开始时删除设置为无效
[self.barButtonitemDelete setEnabled:NO];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


  二、采用提前自定义布局的.xib文件中的UIToolBar,制作的工具栏(删除和添加图片)

(2)自定义的View视图布局,添加UIImage、UILabel控件



 

   

    代码如下:不需要在代码中再去设置添加控件的frame,在.xib文件中已经布局好了。

import "ViewController.h"
#define CONTACE_VIEW_HEIGHT 50
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIToolbar *toolBar;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *barButtonitemDelete;

@end

@implementation ViewController
- (IBAction)addContact:(UIBarButtonItem *)sender
{

//让删除按钮有效
[self.barButtonitemDelete setEnabled:YES];

//在subView中已经有了3个控件
NSInteger count = self.view.subviews.count - 3;
CGRect lastFrame = self.toolBar.frame;

//加载xib文件
NSArray *views = [[NSBundle mainBundle]loadNibNamed:@"contactView" owner:nil options:nil];

//添加contactView
UIView *contactView = [views lastObject];

CGFloat gapY = 5;
CGFloat x = 0;
CGFloat y = lastFrame.origin.y + lastFrame.size.height + (CONTACE_VIEW_HEIGHT+gapY) * count;
CGFloat w = self.view.frame.size.width;
contactView.frame = CGRectMake(x,y,w,CONTACE_VIEW_HEIGHT);

//添加头像
UIImageView *face = (UIImageView *)[contactView viewWithTag:1];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",arc4random_uniform(9)]];
[face setImage:image];

//添加姓名
UILabel *labelName = (UILabel *)[contactView viewWithTag:2];
labelName.text = [NSString stringWithFormat:@"name%d",arc4random_uniform(10)];

[self.view addSubview:contactView];
}

- (IBAction)deleteContact:(UIBarButtonItem *)sender
{
//删除视图
UIView *lastView = [self.view.subviews lastObject];
[lastView removeFromSuperview];

//如果没有了contactView,设置删除按钮无效
if(self.view.subviews.count == 3)
{
[self.barButtonitemDelete setEnabled:NO];
}
}

- (void)viewDidLoad {
[super viewDidLoad];
//开始时删除设置为无效
[self.barButtonitemDelete setEnabled:NO];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: