您的位置:首页 > 产品设计 > UI/UE

UIButton

2015-09-15 08:53 411 查看
UIButton : 是iOS中用于响应用户点击事件的控件,他和UILabel类似,只不过多的是事件的处理功能
在使用上和 UILabel差别不大能够定义的button类型有以下6种
typedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = 0, //自定义风格    
// no button type
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  
// standard system button

UIButtonTypeDetailDisclosure, //<span style="font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 13px; line-height: 14.6666660308838px;">蓝色小箭头按钮,主要做详细说明用</span>
UIButtonTypeInfoLight,//<span style="font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 13px; line-height: 14.6666660308838px;">亮色感叹号</span>
UIButtonTypeInfoDark,//<span style="font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 13px; line-height: 14.6666660308838px;">暗色感叹号</span>
UIButtonTypeContactAdd,//<span style="font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 13px; line-height: 14.6666660308838px;">十字加号按钮</span>

UIButtonTypeRoundedRect = UIButtonTypeSystem,   //圆角矩形 Deprecated, use UIButtonTypeSystem instead
};<span style="color:#333333;">
</span>
//创建空间对象
UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
//2.配置属性
bt.frame = CGRectMake(100, 300, 150, 100);
bt.backgroundColor = [UIColor lightGrayColor];

//(1)设置title
//正常状态下 title的设置
[bt setTitle:@"登陆" forState:UIControlStateNormal];
//高亮状态下 title的设置
[bt setTitle:@"登陆中..." forState:UIControlStateHighlighted];

//(2)添加点击事件
[bt addTarget:self action:@selector(btAction:) forControlEvents:UIControlEventTouchUpInside];
[bt addTarget:self action:@selector(btAction2:) forControlEvents:UIControlEventTouchDown];

//(3)移除点击事件
[bt removeTarget:self action:@selector(btAction2:) forControlEvents:UIControlEventTouchDown];

//(4)设置 title 的颜色
[bt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[bt setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];

//(5)设置阴影颜色
[bt setTitleShadowColor:[UIColor redColor] forState:UIControlStateNormal];
[bt setTitleShadowColor:[UIColor blackColor] forState:UIControlStateHighlighted];

//(6)设置背景图片
[bt setBackgroundImage:[UIImage imageNamed:@"01.png"] forState:UIControlStateNormal];
[bt setBackgroundImage:[UIImage imageNamed:@"02.png"] forState:UIControlStateHighlighted];

//(7)设置前景图片
[bt setImage:[UIImage imageNamed:@"01.png"] forState:UIControlStateNormal];
[bt setImage:[UIImage imageNamed:@"02.png"] forState:UIControlStateHighlighted];
/*
forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现*/
//以下是几种状态
//
enum {
//
UIControlStateNormal = 0, 常规状态显现
//
UIControlStateHighlighted = 1 << 0,高亮状态显现
//
UIControlStateDisabled = 1 << 1,禁用的状态才会显现
//
UIControlStateSelected = 1 << 2,选中状态
//
UIControlStateApplication = 0x00FF0000, 当应用程序标志时
//
UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他
//
};
 


1.UIButton状态:

UIControlStateNormal // 正常状态UIControlStateHighlighted // 高亮状态UIControlStateDisabled// 禁用状态UIControlStateSelected// 选中状态UIControlStateApplication //UIControlStateReserved// 保留状态


2.Uibutton类型:

UIButtonTypeCustom//自定义类型UIButtonTypeRoundedRect //圆角类型UIButtonTypeDetailDisclosure //细节展示按钮UIButtonTypeInfoLight //浅色背景的信息按钮UIButtonTypeInfoDark //暗色背景的信息按钮UIButtonTypeContactAdd // 添加按钮


3.UIButton常用属性

给按钮设置文字时,苹果文档说明,不能使用label对象设置文字的颜色或者阴影颜色,相反必须使用setTitleColor:forState: and setTitleShadowColor:forState:这两个方法才能修改这些值。设置按钮中其他属性依次类推。。。。//设置对应状态的标题内容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;这些事件都是基于触摸、基于值、基于编辑。有如下事件会触发。在点击按钮是按钮是凹下去,然后弹起才触发起事件,按钮的状态有:1. UIControlEventTouchDown // 按下2.3. UIControlEventTouchDownRepeat 多次按下4. UIControlEventTouchUpInside // 在按钮及其一定外围内松开5.6. UIControlEventTouchUpOutside // 按钮外面松开


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个像素的距离。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: