iOS_UI_4_UIButton

来源:互联网 发布:cad网络图标 编辑:程序博客网 时间:2024/05/17 23:45

一. UIButton定义

UIButton(按钮): 用于点击来执行一定功能的控件, 继承于UIContol.

二. UIButton创建

两种创建方式:

// 用 buttonWithType 创建:UIButton *button = [[UIButton buttonWithType:(UIButtonType);button.frame = CGRectMake(20, 20, 280, 40);// 用 initWithFrame 创建:UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 280, 40)];// 此种创建方式只能用默认样式 , 一般不用
/*/* UIButtonType能够定义的button类型有以下6种,typedef enum {UIButtonTypeCustom = 0, //自定义风格UIButtonTypeRoundedRect, //圆角矩形UIButtonTypeDetailDisclosure, //蓝色小箭头按钮,主要做详细说明用UIButtonTypeInfoLight, // 亮色感叹号UIButtonTypeInfoDark, // 暗色感叹号UIButtonTypeContactAdd, // 十字加号按钮 } UIButtonType;*/*/

三. 设置frame

button.frame = CGRectMake(20, 20, 280, 40);[button setFrame:CGRectMake(20,20,50,50)];

四. forState
forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现

// stateenum {UIControlStateNormal = 0, //常规状态显现UIControlStateHighlighted = 1 << 0, //高亮状态显现UIControlStateDisabled = 1 << 1,  //禁用的状态才会显现UIControlStateSelected = 1 << 2,  //选中状态UIControlStateApplication = 0x00FF0000, //当应用程序标志时UIControlStateReserved = 0xFF000000 //为内部框架预留};@property(nonatomic,getter=isEnabled)BOOL enabled;                                // default is YES. if NO, ignores touch events and subclasses may draw differently@property(nonatomic,getter=isSelected)BOOL selected;                              // default is NO may be used by some subclasses or by application@property(nonatomic,getter=isHighlighted)BOOL highlighted;*/

五. 设置标题和标题颜色

[button setTitle:@"点击" forState:UIControlStateNormal];[buttonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];

六. 设置背景图片和前景图片

    //增加背景图片,图片如果小会铺满整个背景    [button setBackgroundImage:[UIImage imageNamed:@"***.png"] forState:UIControlStateNormal];    //增加前景图片,这种方式不会缩放图片,而且会居中    [button setImage:[UIImage imageNamed:@"logo.png"] forState:UIControlStateNormal];    //设置按钮文字,增加的文字会和setImage图片一并居中,图片在左边,文字紧随其后    [button setTitle:@"自定义按钮" forState:UIControlStateNormal];    //如果需要***重新排版这个图片和按钮文字***的位置,则需要重写UIButton类里面的两个函数,点击UIButton可查看    //设置按钮内部图片间距和标题间距     UIEdgeInsets insets; // 设置按钮内部图片间距     insets.top = insets.bottom = insets.right = insets.left = 10;     button.contentEdgeInsets = insets;     button.titleEdgeInsets = insets; // 标题间距     - (CGRect)titleRectForContentRect:(CGRect)contentRect;文字相对于按钮的位置     - (CGRect)imageRectForContentRect:(CGRect)contentRect;图片相对于按钮的位置

七. 设置按钮按下的发光状态

button.showsTouchWhenHighlighted = YES; // 默认为NO不发光

八. 添加事件, 删除事件

// 重点:[button addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];[button removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
// controlEvent    // UIControlEvent    /*       //. 点下去     UIControlEventTouchDown           = 1 <<  0,      // on all touch downs     // . 双击     UIControlEventTouchDownRepeat     = 1 <<  1,      // on multiple touchdowns (tap count > 1)     // .往里拖拽     UIControlEventTouchDragInside     = 1 <<  2,     // .往外拖拽     UIControlEventTouchDragOutside    = 1 <<  3,     // .拖拽确实     UIControlEventTouchDragEnter      = 1 <<  4,     // .拖拽退出     UIControlEventTouchDragExit       = 1 <<  5,     // .点击进入     UIControlEventTouchUpInside       = 1 <<  6,     // .点击退出     UIControlEventTouchUpOutside      = 1 <<  7,     // .触摸取消     UIControlEventTouchCancel         = 1 <<  8,     // . 多与UISlider配合使用     UIControlEventValueChanged        = 1 << 12,     // sliders, etc.     // 下面的不常用     UIControlEventEditingDidBegin     = 1 << 16,     // UITextField     UIControlEventEditingChanged      = 1 << 17,     UIControlEventEditingDidEnd       = 1 << 18,     UIControlEventEditingDidEndOnExit = 1 << 19,     // 'return key' ending editing */
0 0