//创建button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//button的类型 :
// UIButtonTypeCustom = 0,自定义风格
// UIButtonTypeRoundedRect,圆角矩形
// UIButtonTypeDetailDisclosure,蓝色小箭头按钮,主要做详细说明用
// UIButtonTypeInfoLight,亮色感叹号
// UIButtonTypeInfoDark,暗色感叹号
// UIButtonTypeContactAdd,十字加号按钮
//设置圆角
[button.layer setMasksToBounds:YES];
[button.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
//边框宽度
[button.layer setBorderWidth:1.0];
//设置边框颜色有两种方法:
第一种如下:
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 0, 0, 0, 1 });
[button.layer setBorderColor:colorref];//边框颜色
第二种方法如下:
button.layer.borderColor=[UIColor blackColor].CGColor;
button.frame = CGRectMake(100, 100,90, 90);//button的frame
button.backgroundColor = [UIColor cyanColor];//button的背景颜色
[button setBackgroundImage:[UIImage imageNamed:@"man_64.png"] forState:UIControlStateNormal]; //button的背置图(不可调整图片大小)
//在UIButton中有三个对EdgeInsets的设置:ContentEdgeInsets、titleEdgeInsets、imageEdgeInsets
[button setImage:[UIImage imageNamed:@"IconHome@2x.png"] forState:UIControlStateNormal];//给button添加前置图image(可以调整大小位置)
button.imageEdgeInsets = UIEdgeInsetsMake(5,13,21,button.titleLabel.bounds.size.width);//设置image在button上的位置(上top,左left,下bottom,右right)这里可以写负值,对上写-5,那么image就象上移动5个像素
[button setTitle:@"首页" forState:UIControlStateNormal];//设置button的title
//forState的状态
// UIControlStateNormal = 0, 常规状态显现
// UIControlStateHighlighted = 1 << 0, 高亮状态显现
// UIControlStateDisabled = 1 << 1, 禁用的状态才会显现
// UIControlStateSelected = 1 << 2, 选中状态
// UIControlStateApplication = 0x00FF0000, 当应用程序标志时
// UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他
* 默认情况下,当按钮高亮的情况下,图像的颜色会被画深一点,如果这下面的这个属性设置为no,那么可以去掉这个功能
button1.adjustsImageWhenHighlighted = NO;
*跟上面的情况一样,默认情况下,当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置
button1.adjustsImageWhenDisabled = NO;
*下面的这个属性设置为yes的状态下,按钮按下会发光
button1.showsTouchWhenHighlighted = YES;
button.titleLabel.font = [UIFont systemFontOfSize:16];//title字体大小
button.titleLabel.textAlignment = NSTextAlignmentCenter;//设置title的字体居中
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];//设置title在一般情况下为白色字体
[button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];//设置title在button被选中情况下为灰色字体
button.titleEdgeInsets = UIEdgeInsetsMake(71, -button.titleLabel.bounds.size.width-50, 0, 0);//设置title在button上的位置(上top,左left,下bottom,右right)
//[button setContentEdgeInsets:UIEdgeInsetsMake(70, 0, 0, 0)];
//button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//设置button的内容横向居中。。设置content是title和image一起变化
//button的点击事件
[button addTarget:self action:@selector(tap) forControlEvents:UIControlEventTouchUpInside];
[IOS UIButton事件]
UIControlEventTouchDown单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
**UIControlEventTouchDownRepeat**
多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
**UIControlEventTouchDragInside**
当一次触摸在控件窗口内拖动时。
**UIControlEventTouchDragOutside**
****当一次触摸在控件窗口之外拖动时。
UIControlEventTouchDragEnter
****当一次触摸从控件窗口之外拖动到内部时。
UIControlEventTouchDragExit
****当一次触摸从控件窗口内部拖动到外部时。****
UIControlEventTouchUpInside
所有在控件之内触摸抬起事件。
UIControlEventTouchUpOutside
****所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
UIControlEventTouchCancel
所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。
UIControlEventTouchChanged
当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。
UIControlEventEditingDidBegin
当文本控件中开始编辑时发送通知。
UIControlEventEditingChanged
当文本控件中的文本被改变时发送通知。
UIControlEventEditingDidEnd
****当文本控件中编辑结束时发送通知。
UIControlEventEditingDidOnExit
当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。
UIControlEventAlltouchEvents
通知所有触摸事件。
UIControlEventAllEditingEvents
通知所有关于文本编辑的事件。
UIControlEventAllEvents
通知所有事件。
UIButton属性大全
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- // 下面该方法已经在 3.0 之后弃用,如果设置按钮字体大小,可以使用下面第二种的方法进行设置. 第一种:sel...
- button可以设置 titleEdgeInsets属性和 imageEdgeInsets属性来调整其image和...
- UIButton内有两个控件titleLabel和imageView,可以用来显示一个文本和图片,这里的图片区别于...
- UIBtton-自定制按钮 在系统的按钮中,我门可以给按钮添加背景图片,文字的属性,但在现实中我们会见到很多上面是...