1 1、UIButton->UIView 按钮
2 /************UIButton-System(文字样式)**********/
3 //按钮实例化方式:类方法创建
4 /*
5 1、UIButtonTypeSystem=UIButtonTypeRoundedRect iOS7之后
6 2、UIButtonTypeRoundedRect iOS7之前有效,iOS7之后无效果
7 3、UIButtonTypeInfoLight = UIButtonTypeInfoDark = UIButtonTypeDetailDisclosure 蓝色圆圈i
8 4、UIButtonTypeContactAdd 蓝色圆圈+
9 */
10 /*
11 1、UIButtonTypeSystem=UIButtonTypeRoundedRect 文字出现在中间
12 2、系统图片:文字出现在图片的右边
13 */
14 UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
15 button.backgroundColor = [UIColor yellowColor];
16 //类方法创建没有位置和大小,需用frame属性设置位置和大小
17 button.frame = CGRectMake(20, 160, 280, 40);
18 //按钮是否可用:enabled 默认YES YES:可用 NO:不可用
19 button.enabled = YES;
20 //设置文字: setTitle:forState:
21 /*
22 1、UIControlStateNormal 一般状态
23 2、UIControlStateHighlighted 高亮状态
24 3、UIControlStateDisabled 不可用时的状态
25 */
26 [button setTitle:@"button" forState:UIControlStateNormal];
27 // [button setTitle:@"highlight" forState:UIControlStateHighlighted];
28 //不可用时的状态
29 [button setTitle:@"disable" forState:UIControlStateDisabled];
30
31 //文字颜色:setTitleColor:forState:
32 [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
33 [button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
34
35 //按钮文字的大小:.titleLabel.font
36 button.titleLabel.font = [UIFont systemFontOfSize:22.0];
37
38 //按钮添加点击事件 按钮的点击事件必须实现
39 /*
40 1、Target:按钮点击之后我要通知给谁(通知给本类):self
41 2、action:方法、事件 @selector(方法名字)
42 3、按钮要如何点击,点击的方式
43 */
44 //不带参数的写法
45 // [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
46 //带参数的写法
47 [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
48 /*
49 1、UIControlEventTouchUpInside 里进里出
50 2、UIControlEventTouchUpOutside 里进外出
51 3、UIControlEventTouchDragOutside 里进外拖拽
52 4、UIControlEventTouchDragInside 里进里拖拽
53 5、UIControlEventTouchDragExit 拖拽出去
54 6、UIControlEventTouchDragEnter 拖拽出去再返回
55 7、UIControlEventTouchDownRepeat 双击
56 8、UIControlEventTouchDown 点中就有操作
57 */
58
59 //添加到父视图上
60 [self.window addSubview:button];
61
62 //不带参形式的方法实现
63 - (void)buttonClick{
64 NSLog(@"buttonClick");
65 }
66 //带参形式的方法实现
67 - (void)buttonClick:(UIButton *)button{
68 //获取按钮的文字:.currentTitle .titleLabel.text
69 //currentTitle 当按钮带有高亮状态,找到高亮状态的文字;无高亮状态,找到正常状态的文字
70 //titleLabel.text 当高亮状态点击出现时,找到高亮状态的文字;当高亮状态未出现时,找到正常状态的文字。无高亮状态,找到正常状态的文字
71 NSString *str = button.currentTitle;
72 NSString *str2 = button.titleLabel.text;
73 NSLog(@"%@===%@",str,str2);
74 }
75
76 /*********UIButton-Custom(图片类型)**********/
77 //实例化
78 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
79 //位置大小
80 button.frame = CGRectMake(50, 100, 120, 64);
81 //属性
82 button.backgroundColor = [UIColor grayColor];
83 //添加图片:setImage:forState:
84 //当按钮大小小于等于图片大小时,图片压缩或正常显示;当按钮大小大于图片大小时,图片不会拉伸,按原大小显示
85 //文字显示在图片右面
86 [button setImage:image forState:UIControlStateNormal];
87 //设置背景图片
88 //当按钮大小小于等于图片大小时,图片压缩或正常显示;当按钮大小大于图片大小时,图片会被拉伸,按按钮大小显示
89 //文字显示在图片上面,按钮的正中间
90 // [button setBackgroundImage:image forState:UIControlStateNormal];
91 //设置文字 点击时按钮文字不会变化
92 [button setTitle:@"button" forState:UIControlStateNormal];
93 //添加点击事件
94 [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
95 //添加到父视图上
96 [self.window addSubview:button];
97
98 2、UIImageView->UIView 展示图片的控件
99 UIImage->NSObject 图片类
100 /*************UIImage***************/
101 //由名字直接读取图片 [UIImage imageNamed:@"1.png"]
102 //UIImage不是控件,只是图片类
103 UIImage *image = [UIImage imageNamed:@"1.png"];
104 // image.size.width 图片的宽
105 // image.size.height 图片的高
106
107 /**************UIImageView*****************/
108 //UIImageView->UIView
109 //实例化
110 UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 64, 64)];
111 //属性
112 imageView.backgroundColor = [UIColor grayColor];
113 //imageView的图片属性
114 imageView.image = [UIImage imageNamed:@"1.png"];
115 //添加到父视图
116 [self.window addSubview:imageView];