hereiam
 1 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];//初始化button,选择button类型
 2 btn.frame = CGRectMake(30, 360, 90, 35);//大小和位置
 3 
 4 [btn setTitle:@"ZoomIn" forState:UIControlStateNormal];//正常状况下button显示的标题
 5 [btn setTitle:@"ZoomIn" forState:UIControlStateHighlighted];//高亮显示时button的标题
 6 [btn addTarget:self action:@selector(zoomInAction:) forControlEvents:UIControlEventTouchUpInside];//button被按下又抬起后发生的事件
 7 //@selector可以理解为"选择子",selector是一个指针变量,类似于sender。 这里是将method的方法指定给新建的这个btn。
 8 /*在 method 方法里可以将 sender 看作是 btn 了 
 9 比如设置btn的hidden属性等等 
10 btn.hidden = YES; 
11 这样btn被隐藏了
12 /*
13 // 通过背景图片设置按钮高亮
14 IImage *normalImage = [UIImage imageNamed:@"NormalBlueButton.png"]; 
15     UIImage *highlightedImage = [UIImage imageNamed:@"HighlightedBlueButton"];
16     self.myButton = [UIButton buttonWithType:UIButtonTypeCustom];
17     self.myButton.frame = CGRectMake(110.0f,200.0f,100.0f, 37.0f);
18     [self.myButton  setBackgroundImage:normalImage forState:UIControlStateNormal];
19     [self.myButton setBackgroundImage:highlightedImage  forState:UIControlStateHighlighted];
20     [self.myButton setTitle:@"Normal" forState:UIControlStateNormal];
21     [self.myButton setTitle:@"Pressed" forState:UIControlStateHighlighted];
22 //这里创建一个圆角矩形的按钮
23     UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
24      
25 
26 //    能够定义的button类型有以下6种,
27 
28 //    typedef enum {
29 
30 //        UIButtonTypeCustom = 0,          自定义风格
31 
32 //        UIButtonTypeRoundedRect,         圆角矩形
33 
34 //        UIButtonTypeDetailDisclosure,    蓝色小箭头按钮,主要做详细说明用
35 
36 //        UIButtonTypeInfoLight,           亮色感叹号
37 
38 //        UIButtonTypeInfoDark,            暗色感叹号
39 
40 //        UIButtonTypeContactAdd,          十字加号按钮
41 
42 //    } UIButtonType;
43      
44 
45     //给定button在view上的位置
46 
47     button1.frame = CGRectMake(20, 20, 280, 20);
48 
49     //button背景色
50 
51     button1.backgroundColor = [UIColor clearColor];
52 
53     //设置button填充图片
54 
55     //[button1 setImage:[UIImage imageNamed:@"btng.png"] forState:UIControlStateNormal];
56  
57 
58     //设置button标题
59 
60     [button1 setTitle:@"点击" forState:UIControlStateNormal];
61 
62     /* forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现*/
63     //以下是几种状态
64 //    enum {
65 //        UIControlStateNormal       = 0,         常规状态显现             
66 //        UIControlStateHighlighted  = 1 << 0,    高亮状态显现   
67 //        UIControlStateDisabled     = 1 << 1,    禁用的状态才会显现
68 //        UIControlStateSelected     = 1 << 2,    选中状态             
69 //        UIControlStateApplication  = 0x00FF0000, 当应用程序标志时           
70 //        UIControlStateReserved     = 0xFF000000  为内部框架预留,可以不管他            
71 //    };
72      
73     /*
74      * 默认情况下,当按钮高亮的情况下,图像的颜色会被画深一点,如果这下面的这个属性设置为no,
75      * 那么可以去掉这个功能
76     */
77     button1.adjustsImageWhenHighlighted = NO;
78     /*跟上面的情况一样,默认情况下,当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置*/
79     button1.adjustsImageWhenDisabled = NO;
80     /* 下面的这个属性设置为yes的状态下,按钮按下会发光*/
81     button1.showsTouchWhenHighlighted = YES;
82      
83 
84     /* 给button添加事件,事件有很多种,我会单独开一篇博文介绍它们,下面这个时间的意思是
85      按下按钮,并且手指离开屏幕的时候触发这个事件,跟web中的click事件一样。
86      触发了这个事件以后,执行butClick:这个方法,addTarget:self 的意思是说,这个方法在本类中
87      也可以传入其他类的指针*/
88     [button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
89 
90     //显示控件
91     [self.view addSubview:button1];

1.创建 
+ buttonWithType: // 创建并返回一个特定风格的按钮 
 
2.设置标题 
  buttonType  property 
  titleLabel  property // 显示按钮当前标题的视图(只读).虽然它是只读的,但是它的属性是可读写的.它的属性在按钮还没有显示之前就有返回值.系统按钮这些值为nil 
  reversesTitleShadowWhenHighlighted  property   // 确定按钮高亮时是否改变阴影的Bool值.默认时NO,当为YES时,阴影在雕刻与浮雕感之间变化(差不多就是去正常offset的相反数作为新的offset) 
– setTitle:forState: // 设置不同状态的标题,不同状态默认的标题是UIControlStateNormal时的值.若normal状态的值没有设定,默认是个系统值,所以你只要要设置normal状态 
– setTitleColor:forState: // 设置不同状态时的标题颜色.不同状态默认的标题颜色是UIControlStateNormal时的值.若normal状态的值没有设定,默认是个系统值,所以你只要要设置normal状态 
– setTitleShadowColor:forState: // 设置不同状态时的标题阴影颜色.不同状态默认的标题阴影颜色是UIControlStateNormal时的值.若normal状态的值没有设定,默认是个系统值,所以你只要要设置normal状态 
– titleColorForState: // 返回不同状态时标题颜色. 
– titleForState: // 返回不同状态时的标题 
– titleShadowColorForState: // 返回不同状态时的标题阴影颜色 
  font  property Deprecated in iOS 3.0 
  lineBreakMode  property Deprecated in iOS 3.0 
  titleShadowOffset  property Deprecated in iOS 3.0 
 
3.设置按钮图片 
  adjustsImageWhenHighlighted  property // 确定当按钮高亮时图片是否改变的BOOL值,为真时图片随按钮高亮而高亮 
  adjustsImageWhenDisabled  property // 确定当按钮高亮时图片是否改变的BOOL值,为真时图片随按钮失效而变暗 
  showsTouchWhenHighlighted  property // 控制当按钮按下时是否闪光的BOOL值.默认NO,YES时按下会有白色光点.图片和按钮事件的不会因闪光改变. 
– backgroundImageForState: // 返回某个按钮状态下使用的背景图片. 
– imageForState: // 返回某个状态下的按钮图片. 
– setBackgroundImage:forState: // 设置特定状态的背景图片,默认都是normal  
– setImage:forState: // 设置特定状态的图片,默认都是normal 
 
4.设置边界距离 
  contentEdgeInsets  property // 按钮content内外线边缘绘制区域.content包含按钮图片和标题 
Use this property to resize and reposition the effective drawing rectangle for the button content. The content comprises the button image and button title. You can specify a different value for each of the four insets (top, left, bottom, right). A positive value shrinks, or insets, that edge—moving it closer to the center of the button. A negative value expands, or outsets, that edge. Use the UIEdgeInsetsMake function to construct a value for this property. The default value is UIEdgeInsetsZero. 
 
  titleEdgeInsets  property // 按钮标题绘制区域控制. 
Use this property to resize and reposition the effective drawing rectangle for the button title. You can specify a different value for each of the four insets (top, left, bottom, right). A positive value shrinks, or insets, that edge—moving it closer to the center of the button. A negative value expands, or outsets, that edge. Use the UIEdgeInsetsMake function to construct a value for this property. The default value is UIEdgeInsetsZero. 
  imageEdgeInsets  property // 图片绘制偏移区域控制. 
Use this property to resize and reposition the effective drawing rectangle for the button image. You can specify a different value for each of the four insets (top, left, bottom, right). A positive value shrinks, or insets, that edge—moving it closer to the center of the button. A negative value expands, or outsets, that edge. Use the UIEdgeInsetsMake function to construct a value for this property. The default value is UIEdgeInsetsZero. 
 
5.得到当前状态 
  currentTitle  property // 当前按钮上显示的标题(只读),当按钮状态改变时值自动改变.值可以为nil. 
  currentTitleColor  property // 当前标题颜色(只读).此值要保证不为nil,默认是白色. 
  currentTitleShadowColor  property // 标题的阴影颜色(只读).默认是白色. 
  currentImage  property // 当前按钮上的图片(只读).可以是nil. 
  currentBackgroundImage  property // 当前按钮背景图片(只读).可以是nil. 
  imageView  property // 按钮上的图片视图(只读).虽然它是只读的,但是他的属性是可读写的.imageView的属性在按钮还没有显示前就有值了.系统按钮这些值是nil 
 
6.得到尺寸 
– backgroundRectForBounds: // 返回背景绘制区域. 
– contentRectForBounds: // 返回内容绘制区域.内容区域是显示图片和标题及他们特定对齐缩放等的范围. 
– titleRectForContentRect: // 返回标题的绘制区域. 
– imageRectForContentRect: // 返回图片的绘制区域. 
 
 
 
7.其他 
按钮种类 
typedef enum { 
   UIButtonTypeCustom = 0,  // 没有风格 
   UIButtonTypeRoundedRect, // 圆角风格按钮 
   UIButtonTypeDetailDisclosure, //  
   UIButtonTypeInfoLight, // 明亮背景的信息按钮 
   UIButtonTypeInfoDark, // 黑暗背景的信息按钮 
   UIButtonTypeContactAdd, // 
} UIButtonType; 
 
按钮状态: 
 
UIControlEventTouchDown      // 按下    
UIControlEventTouchDownRepeat  // 多次按下   
UIControlEventTouchDragInside   // 保持按下然后在按钮及其一定的外围拖动 
UIControlEventTouchDragOutside  // 保持按下,在按钮外面拖动 
UIControlEventTouchDragEnter  // DragOutside进入DragInside触发 
UIControlEventTouchDragExit  // in到out触发 
UIControlEventTouchUpInside // 在按钮及其一定外围内松开 
UIControlEventTouchUpOutside // 按钮外面松开 
UIControlEventTouchCancel   // 点击取消 

分类:

技术点:

相关文章: