UIButton

第一、UIButton的定义

UIButton *button=[[UIButton buttonWithType:(UIButtonType);

能够定义的button类型有以下6种,

typedef enum {

UIButtonTypeCustom = 0, 自定义风格

UIButtonTypeRoundedRect, 圆角矩形

UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用

UIButtonTypeInfoLight, 亮色感叹号

UIButtonTypeInfoDark, 暗色感叹号

UIButtonTypeContactAdd, 十字加号按钮

} UIButtonType;


第二、设置frame

button1.frame = CGRectMake(20, 20, 280, 40);

[button setFrame:CGRectMake(20,20,50,50)];


第三、button背景色

button1.backgroundColor = [UIColor clearColor];

[button setBackgroundColor:[UIColor blueColor]];


第四、state状态

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

enum {

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填充图片和背景图片

[buttonsetImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];

[buttonsetBackgroundImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];


第六、设置button标题和标题颜色

[button1 setTitle:@"点击" forState:UIControlStateNormal];

[buttonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];


第七、设置按钮按下会发光

button.showsTouchWhenHighlighted=NO;


第八、添加或删除事件处理

[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];

[btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];


第九、 设置按钮内部图片间距和标题间距

UIEdgeInsets insets; // 设置按钮内部图片间距

insets.top = insets.bottom = insets.right = insets.left = 10;

bt.contentEdgeInsets = insets;

bt.titleEdgeInsets = insets; // 标题间距


第十   把Button按钮添加到View上

[self.view addSubview:button1];


第十一   iOS UIButton事件:

UIControlEventTouchDown

单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。

UIControlEventTouchDownRepeat

多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。

UIControlEventTouchDragInside

当一次触摸在控件窗口内拖动时。

UIControlEventTouchDragOutside

当一次触摸在控件窗口之外拖动时。

UIControlEventTouchDragEnter

当一次触摸从控件窗口之外拖动到内部时。

UIControlEventTouchDragExit

当一次触摸从控件窗口内部拖动到外部时。

UIControlEventTouchUpInside

所有在控件之内触摸抬起事件。

UIControlEventTouchUpOutside

所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。

UIControlEventTouchCancel

所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。

UIControlEventTouchChanged

当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。

UIControlEventEditingDidBegin

当文本控件中开始编辑时发送通知。

UIControlEventEditingChanged

当文本控件中的文本被改变时发送通知。

UIControlEventEditingDidEnd

当文本控件中编辑结束时发送通知。

UIControlEventEditingDidOnExit

当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。

UIControlEventAlltouchEvents

通知所有触摸事件。

UIControlEventAllEditingEvents

通知所有关于文本编辑的事件。

UIControlEventAllEvents

通知所有事件。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 各位童鞋们, UIButton是iOS中常用的控件,下面来详细介绍它的使用方法和以及开发中需要注意的问题. UIB...
    我与太阳肩并肩阅读 1,330评论 2 17
  • 这段代码动态的创建了一个UIButton,并且把相关常用的属性都列举了.希望对大家有用.//这里创建一个圆角矩形的...
    大荣纸阅读 2,402评论 0 0
  • 这段代码动态的创建了一个UIButton,并且把相关常用的属性都列举了//这里创建一个圆角矩形的按钮UIButto...
    AAup阅读 354评论 0 1
  • 前言:UI控件整理之UIButton 一、显示图片(复选框) UIButton *button = [UIButt...
    心如止水的鱼阅读 294评论 0 0
  • 授课技巧学习心得 这周学习授课技巧,让我留下深刻印象的如下: 1.你是谁,是由你对面站着的人决定的。你的好与坏也不...
    虾米82阅读 198评论 0 0