UIButton总结

1.UIButton状态:

UIControlStateNormal          // 正常状态

UIControlStateHighlighted     // 高亮状态

UIControlStateDisabled        // 禁用状态

UIControlStateSelected        // 选中状态

UIControlStateApplication     //应用程序状态    

UIControlStateReserved        // 保留状态

2.UIButton类型:

UIButtonTypeCustom            //自定义类型

添加图片: wps82A9.tmp 灰色背景颜色:wps82AA.tmp

UIButtonTypeRoundedRect       //圆角类型

wps82AB.tmp

UIButtonTypeDetailDisclosure   //细节展示按钮

wps82BB.tmp

UIButtonTypeInfoLight          //浅色背景的信息按钮

wps82BC.tmp

UIButtonTypeInfoDark           //暗色背景的信息按钮

wps82CD.tmp

UIButtonTypeContactAdd         // 添加按钮

wps82CE.tmp

3.UIButton常用属性

给按钮设置文字时,苹果文档说明,不能使用label对象设置文字的颜色或者阴影颜色,相反必须使用setTitleColor:forState: and setTitleShadowColor:forState:这两个方法才能修改这些值。

wps82CF.tmp

设置按钮中其他属性依次类推。。。。

//设置对应状态的标题内容default is nil. title is assumed to be single line

- (void)setTitle:(NSString *)title forState:(UIControlState)state; 

//设置对应状态的标题颜色

- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;  

//设置对应状态的标题阴影颜色

- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;         

//设置对应状态的按钮的图片

- (void)setImage:(UIImage *)image forState:(UIControlState)state;       

//设置对应状态的按钮背景图片

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;

添加事件

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

这些事件都是基于触摸、基于值、基于编辑。有如下事件会触发。

在点击按钮是按钮是凹下去,然后弹起才触发起事件,按钮的状态有:

UIControlEventTouchDown

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

UIControlEventTouchDownRepeat

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

UIControlEventTouchDragInside

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

UIControlEventTouchDragOutside

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

UIControlEventTouchDragEnter

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

UIControlEventTouchDragExit

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

UIControlEventTouchUpInside

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

UIControlEventTouchUpOutside

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

UIControlEventTouchCancel

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

UIControlEventTouchChanged

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

UIControlEventEditingDidBegin

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

UIControlEventEditingChanged

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

UIControlEventEditingDidEnd

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

UIControlEventEditingDidOnExit

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

UIControlEventAlltouchEvents

通知所有触摸事件。

UIControlEventAllEditingEvents

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

UIControlEventAllEvents

通知所有事件。

4.adjustsImageWhenDisabled

当按钮禁用的情况下,图像的颜色会被画深一点,默认为YES。

5.adjustsImageWhenHighlighted

当按钮高亮的情况下,图像的颜色会被画深一点,默认为YES。

6.showsTouchWhenHighlighted

button.showsTouchWhenHighlighted=YES;点击时的闪光效果会被前景图片遮住中间部分;

6.contentEdgeInsets

设置按钮的内部内容(包含按钮图片和标题)离按钮边缘上下左右的距离。

7.按钮实例

1.有些时候我们想让UIButton的title居左对齐,我们设置
btn.textLabel.textAlignment = UITextAlignmentLeft

是没有作用的,我们需要设置

btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;

但是问题又出来,此时文字会紧贴到左边框,我们可以设置

btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);

使文字距离左边框保持10个像素的距离。

转载于:https://my.oschina.net/khakiloveyty/blog/395935

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值