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
时间: 2024-08-06 03:46:17

iOS:UIToolBar控件的使用的相关文章

iOS开发UI篇—DatePicker和UIToolBar控件简单介绍

iOS开发UI篇—DatePicker和UIToolBar控件简单介绍 一.Date Picker控件 1.简单介绍: Date Picker显示时间的控件 有默认宽高,不用设置数据源和代理 如何改成中文的? (1)查看当前系统是否为中文的,把模拟器改成是中文的 (2)属性,locale选择地区 如果默认显示不符合需求.时间有四种模式可以设置,在model中进行设置 时间可以自定义(custom). 设置最小时间和最大时间,超过就会自动回到最小时间. 最大的用途在于自定义键盘:弹出一个日期选择器

iOS常用控件尺寸大集合

元素控件 尺寸(pts) Window(含状态栏) 320 x 480 Status Bar的高度 20 Navigation Bar的高度 44 含Prompt的Navigation Bar的高度 74 Navigation Bar的图标 20×20(透明的png) Tool Bar的高度 44 Tool Bar的图标 20×20(透明的png) Tab Bar的高度 49 Tab Bar的图标 30×30(透明的png) 竖直时键盘的高度 216.252(iOS 5+的中文键盘) 水平时键盘

iOS第三方控件

一.SIAlertView https://github.com/Sumi-Interactive/SIAlertView 感言: 扁平化设计的对话框(UIAlertView),对话框的弹出与消失的动画很不错,可以自定义对话框的外观 iOS第三方控件

Objective-C:UIToolBar控件的使用

UIToolBar控件:是经常使用的一个工具条控件,在它里面可以添加很多其他控件,UILabel.UIButton.UIImage.UIBarButtonItem.UITextField等等...可以用来对视图view中控件进行自定义的布局.视图布局完成后,可以直接拿来用,不用再用代码去控制控件的坐标和大小,方便而且准确. 一.采用系统默认.xib文件中的UIToolBar制作的增删条(删除和添加图片) (1)添加有两个标签Label的系统设置的UIToolBar 代码如下:需要在代码中为添加的

iOS UITextField控件总结

iOS UITextField控件总结 先声明下面总结不是自己写的. //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)]; //设置边框样式,只有设置了才会显示边框样式 text.borderStyle = UITextBorderStyleRoundedRect; typedef enum { UITextBorderStyleNone

iOS基础控件UINavigationController中的传值

iOS基础控件UINavigationController中的传值,代理传值,正向传值,反向传值 #import <UIKit/UIKit.h> //声明一个协议 @protocol SendValue<NSObject> //定义一个方法 - (void)sendBtnTitle:(NSString *)title; @end @interface FirstViewController : UIViewController // 定义代理 @property (nonatomi

IOS Ui控件 修改位置和尺寸,代码添加控件

所有的UI控件最终都继承自UIView,UI控件的公共属性都定义在UIView中, UIView的常见属性 UIView *superview; 获得自己的父控件对象 NSArray *subviews; 获得自己的所有子控件对象 NSInteger tag; 控件的ID(标识),父控件可以通过tag来找到对应的子控件 CGAffineTransform transform; 控件的形变属性(可以设置旋转角度.比例缩放.平移等属性) CGRect frame; 控件所在矩形框在父控件中的位置和尺

IOS—UITextFiled控件详解

IOS—UITextFiled控件详解 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)]; //设置边框样式,只有设置了才会显示边框样式 text.borderStyle = UITextBorderStyleRoundedRect; typedef enum { UITextBorderStyleNone, UITextBorderS

[iOS基础控件 - 5.5] 代理设计模式 (基于”APP列表&quot;练习)

A.概述 在"[iOS基础控件 - 4.4] APP列表 进一步封装,初见MVC模式”上进一步改进,给“下载”按钮加上效果.功能 1.按钮点击后,显示为“已下载”,并且不可以再按 2.在屏幕中间弹出一个消息框,通知消息“xx已经被安装”,慢慢消失 3.消息框样式为圆角半透明 B.不使用代理模式,使用app空间组和主View之间的父子View关系 1.在主View中创建一个消息框 主View控制器:ViewController.m 1 // 创建下载成功消息框 2 CGFloat labelWid