iOS:UIToolBar控件的使用

简介:

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

复制代码
 1 #import "ViewController.h"
 2 #define CONTACE_VIEW_HEIGHT 50
 3 @interface ViewController ()
 4 @property (weak, nonatomic) IBOutlet UIToolbar *toolBar;
 5 @property (weak, nonatomic) IBOutlet UIBarButtonItem *barButtonitemDelete;
 6 
 7 @end
 8 
 9 @implementation ViewController
10 - (IBAction)addContact:(UIBarButtonItem *)sender
11 {
12     
13     //让删除按钮有效
14     [self.barButtonitemDelete setEnabled:YES];
15     
16     //在subView中已经有了3个控件
17     NSInteger count = self.view.subviews.count - 3;
18     CGRect lastFrame = self.toolBar.frame;
19     
20     UIView *contactView = [[UIView alloc]init];
21     CGFloat gapY = 5;
22     CGFloat x = 0;
23     CGFloat y = lastFrame.origin.y + lastFrame.size.height + (CONTACE_VIEW_HEIGHT+gapY) * count;
24     CGFloat w = self.view.frame.size.width;
25     
26     contactView.frame = CGRectMake(x,y,w,CONTACE_VIEW_HEIGHT);
27     
28     
29     //添加头像
30     UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",arc4random_uniform(9)]];
31     UIImageView *face = [[UIImageView alloc]initWithImage:image];
32     face.frame = CGRectMake(10, 0,50,50);
33     [contactView addSubview:face];
34     
35     //添加姓名
36     UILabel *labelName = [[UILabel alloc]init];
37     labelName.text = [NSString stringWithFormat:@"name%d",arc4random_uniform(10)];
38     labelName.frame = CGRectMake(10+image.size.width+50, 0, 100, 50);
39     [contactView addSubview:labelName];
40     
41     
42     contactView.backgroundColor = [UIColor lightGrayColor];
43     
44     [self.view addSubview:contactView];
45 }
46 - (IBAction)deleteContact:(UIBarButtonItem *)sender
47 {
48     //删除视图
49     UIView *lastView = [self.view.subviews lastObject];
50     [lastView removeFromSuperview];
51     
52     //如果没有了contactView,设置删除按钮无效
53     if(self.view.subviews.count == 3)
54     {
55         [self.barButtonitemDelete setEnabled:NO];
56     }
57 }
58 
59 - (void)viewDidLoad {
60     [super viewDidLoad];
61     //开始时删除设置为无效
62     [self.barButtonitemDelete setEnabled:NO];
63 }
64 
65 - (void)didReceiveMemoryWarning {
66     [super didReceiveMemoryWarning];
67     // Dispose of any resources that can be recreated.
68 }
复制代码

 

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

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

    

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

复制代码
 1 import "ViewController.h"
 2 #define CONTACE_VIEW_HEIGHT 50
 3 @interface ViewController ()
 4 @property (weak, nonatomic) IBOutlet UIToolbar *toolBar;
 5 @property (weak, nonatomic) IBOutlet UIBarButtonItem *barButtonitemDelete;
 6 
 7 @end
 8 
 9 @implementation ViewController
10 - (IBAction)addContact:(UIBarButtonItem *)sender
11 {
12     
13     //让删除按钮有效
14     [self.barButtonitemDelete setEnabled:YES];
15     
16     //在subView中已经有了3个控件
17     NSInteger count = self.view.subviews.count - 3;
18     CGRect lastFrame = self.toolBar.frame;
19     
20     //加载xib文件
21     NSArray *views = [[NSBundle mainBundle]loadNibNamed:@"contactView" owner:nil options:nil];
22     
23     //添加contactView
24     UIView *contactView = [views lastObject];
25 
26     
27     CGFloat gapY = 5;
28     CGFloat x = 0;
29     CGFloat y = lastFrame.origin.y + lastFrame.size.height + (CONTACE_VIEW_HEIGHT+gapY) * count;
30     CGFloat w = self.view.frame.size.width;
31     contactView.frame = CGRectMake(x,y,w,CONTACE_VIEW_HEIGHT);
32     
33     
34     //添加头像
35     UIImageView *face = (UIImageView *)[contactView viewWithTag:1];
36     UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",arc4random_uniform(9)]];
37     [face setImage:image];
38 
39 
40     
41     //添加姓名
42     UILabel *labelName = (UILabel *)[contactView viewWithTag:2];
43     labelName.text = [NSString stringWithFormat:@"name%d",arc4random_uniform(10)];
44     
45     [self.view addSubview:contactView];
46 }
47 
48 - (IBAction)deleteContact:(UIBarButtonItem *)sender
49 {
50     //删除视图
51     UIView *lastView = [self.view.subviews lastObject];
52     [lastView removeFromSuperview];
53     
54     //如果没有了contactView,设置删除按钮无效
55     if(self.view.subviews.count == 3)
56     {
57         [self.barButtonitemDelete setEnabled:NO];
58     }
59 }
60 
61 - (void)viewDidLoad {
62     [super viewDidLoad];
63     //开始时删除设置为无效
64     [self.barButtonitemDelete setEnabled:NO];
65 }
66 
67 - (void)didReceiveMemoryWarning {
68     [super didReceiveMemoryWarning];
69     // Dispose of any resources that can be recreated.
70 }
71 
72 @end
复制代码

 

程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!


本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4767887.html,如需转载请自行联系原作者
目录
相关文章
|
iOS开发
iOS 多个滚动控件嵌套Demo
iOS 多个滚动控件嵌套Demo
171 0
|
iOS开发
iOS 常用的 上下左右 拉刷新控件
iOS 常用的 上下左右 拉刷新控件
212 0
|
iOS开发
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
254 0
iOS开发 - 写一个刷新的控件(未封装,适合新手学习,查看原理)
|
安全 iOS开发
iOS小技能:下拉刷新控件的适配
1. 下拉顶部背景色设置: 往tableView的父控件添加拉伸背景视图 2. present 半屏适配 iOS13 modalPresentationStyle属性默认不是全屏样式`UIModalPresentationFullScreen`,而是半屏样式,需要根据需求手动设置。 present 半屏,会导致列表下拉刷新失效。
426 0
iOS小技能:下拉刷新控件的适配
|
iOS开发
iOS小技能:自动布局实现兄弟控件N等分且宽高比例是1:N(xib 上实现)
本文为 iOS视图约束专题的第三篇:xib上使用自动布局教程
356 0
|
iOS开发
iOS短信验证码控件,自动输入回调两次解决办法
iOS短信验证码控件,自动输入回调两次解决办法
726 0
|
iOS开发
IOS的UITableView控件简单使用
IOS的UITableView控件简单使用
322 0
|
存储 前端开发 程序员
iOS开发:实现点击常用控件弹出地区选择框(万能方法)
在iOS开发中会遇到一些选择选项的需求,而且点击一个控件弹出一个选择框,选择之后展示到前端,然后再把选择的内容传给后台或者做本地存储。这个需求对于大多数开发者来说可以为小儿科,但是作为一个爱记录的程序猿来说相当可贵,所以还是那句话,只分享给有缘人,大牛可以飘过,不喜勿喷请走开。
547 0
iOS开发:实现点击常用控件弹出地区选择框(万能方法)
|
数据可视化 程序员 iOS开发
iOS开发:用XIB拖控件关联时报错:“Could not insert new outlet connection…”解决方法
在iOS开发过程中,尤其是iOS开发初期,会遇到各种各样的错误,有些错误是开发者的不熟悉或者疏忽大意造成的,还有些是无厘头的错误,可以通过重启Xcode或者重启电脑就可解决。
474 0
iOS开发:用XIB拖控件关联时报错:“Could not insert new outlet connection…”解决方法
|
iOS开发
iOS开发-加在透明视图上的控件会透明
iOS开发-加在透明视图上的控件会透明
256 0

热门文章

最新文章