UIButton

来源:互联网 发布:显示毫秒的网络时钟 编辑:程序博客网 时间:2024/06/05 16:37

UIButton

UIButton相关常用方法和属性

能够定义的button类型有以下6种:
typedef NS_ENUM(NSInteger, UIButtonType) {

    UIButtonTypeCustom               自定义风格            UIButtonTypeDetailDisclosure     蓝色小箭头按钮    UIButtonTypeInfoLight            亮色感叹号    UIButtonTypeInfoDark             暗色感叹号    UIButtonTypeContactAdd           十字加号按钮    UIButtonTypeRoundedRect          圆角矩形 (= UIButtonTypeSystem)

}

创建一个普通显示文字的按钮

 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

创建一个显示图片的按钮

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];    button.frame = CGRectMake(100, 200, 100, 100);    //设置不同状态下的图片    [button setImage:[UIImage imageNamed:@"Off"] forState:UIControlStateNormal];    //高亮    [button setImage:[UIImage imageNamed:@"On"] forState:UIControlStateHighlighted];    //选中状态    [button setImage:[UIImage imageNamed:@"On"] forState:UIControlStateSelected];

设置位置

    button.frame = CGRectMake(100, 100, 80, 40);

设置不同状态下的文字

   UIControlStateNormal        正常状态   UIControlStateHighlighted   高亮状态   UIControlStateSelected      选中状态   UIControlStateDisabled      不可用状态
 [button setTitle:@"正常状态" forState:UIControlStateNormal]; [button setTitle:@"高亮状态" forState:UIControlStateHighlighted];     [button setTitle:@"选中状态" forState:UIControlStateSelected];

设置按钮不同状态下的颜色

    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];    [button setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];

统一设置所有颜色(优先级比较低)

    [button setTintColor:[UIColor blueColor]];

设置背景颜色

 button.backgroundColor = [UIColor redColor];

设置字体大小

    button.titleLabel.font = [UIFont systemFontOfSize:20];

添加响应事件

[button addTarget:<#(nullable id)#> action:<#(nonnull SEL)#> forControlEvents:<#(UIControlEvents)#>];addTarget:谁来实现这个函数,实现者对象就是"谁",一般是selfaction:@selector(selector) 函数对象,当按钮满足p3事件类型的时候调用forControlEvents:<#(UIControlEvents)#> 事件处理类型函数  
    [button addTarget:self action:@selector(pressAction:) forControlEvents:UIControlEventTouchUpInside];   

响应事件的枚举值的详解

typedef NS_OPTIONS(NSUInteger, UIControlEvents) :


UIControlEventTouchDown单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。UIControlEventTouchDownRepeat多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。UIControlEventTouchDragInside当一次触摸在控件窗口内拖动时。UIControlEventTouchDragOutside当一次触摸在控件窗口之外拖动时。UIControlEventTouchDragEnter当一次触摸从控件窗口之外拖动到内部时。UIControlEventTouchDragExit当一次触摸从控件窗口内部拖动到外部时。UIControlEventTouchUpInside所有在控件之内触摸抬起事件。UIControlEventTouchUpOutside所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。UIControlEventTouchCancel所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。UIControlEventTouchChanged当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。UIControlEventEditingDidBegin当文本控件中开始编辑时发送通知。UIControlEventEditingChanged当文本控件中的文本被改变时发送通知。UIControlEventEditingDidEnd当文本控件中编辑结束时发送通知。UIControlEventEditingDidOnExit当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。UIControlEventAlltouchEvents通知所有触摸事件。UIControlEventAllEditingEvents通知所有关于文本编辑的事件。UIControlEventAllEvents通知所有事件。

0 0
原创粉丝点击