- // 设置按钮类型,此处为圆角按钮
- UIButton *writeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- // 设置和大小
- CGRect frame = CGRectMake(100.0f, 100.0f, 140.0f, 50.0f);
- // 将frame的位置大小复制给Button
- writeButton.frame = frame;
- //-----------------------------------------------
- // 给Button添加标题
- [writeButton setTitle:@"代码按钮" forState:UIControlStateNormal];
- // 设置按钮背景颜色
- writeButton.backgroundColor = [UIColor clearColor];
- // 设置按钮标题文字对齐方式,此处为左对齐
- writeButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
- //使文字距离做边框保持10个像素的距离。
- writeButton.contentEdgeInsets = UIEdgeInsetsMake(0,30, 0, 0);
- //----------------------------------------------------
- /******************************************************
- //此处类容目的掩饰代码代码操作按钮一些属性,如果设置按钮背景为图片可以将此处注释取消,注释掉上没横线范围类代码,进行测试
- // 设置按钮背景图片
- UIImage *image= [UIImage imageNamed:@"background.png"];
- [writeButton setBackgroundImage:image forState:UIControlStateNormal];
- // 按钮的相应事件
- *****************************************************/
- [writeButton addTarget:self action:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:writeButton];
- }
- <span style="color:rgb(166,19,144)">typedef</span> <span style="color:rgb(166,19,144)">enum</span> <span style="color:rgb(0,34,0)">{</span>
- UIButtonTypeCustom <span style="color:rgb(0,34,0)">=</span> <span style="color:rgb(36,0,217)">0</span>, <span style="color:rgb(17,116,10)"><em>// 没有风格</em></span>
- UIButtonTypeRoundedRect, <span style="color:rgb(17,116,10)"><em>// 圆角风格按钮</em></span>
- UIButtonTypeDetailDisclosure, <span style="color:rgb(17,116,10)"><em>// </em></span>
- UIButtonTypeInfoLight, <span style="color:rgb(17,116,10)"><em>// 明亮背景的信息按钮</em></span>
- UIButtonTypeInfoDark, <span style="color:rgb(17,116,10)"><em>// 黑暗背景的信息按钮</em></span>
- UIButtonTypeContactAdd, <span style="color:rgb(17,116,10)"><em>//</em></span>
- <span style="color:rgb(0,34,0)">}</span> UIButtonType;
1.预置按钮类型
sdk提供了5个预置按钮类型:Detail Disclosure,Info Light,Info Dark,Contact Add,Rounded Rectangle。它们添加到sdk中首先是为了方便苹果公司自己。
构造方式:[UIButton buttonWithType:UIButtonTypeContactAdd];
2.显示系统私有UIButton风格
指定 值为100 以上的UIButton的buttonWithType可以得到非公开的按钮风格,像红色按钮,黑色按钮,箭头返回按钮等。
对于某种风格,可以用[button setTintColor:[UIColor blueColor]];来改变按钮颜色。
参考
http://zhaohaiyang.blog.51cto.com/2056753/756082
3.图片和文字环绕
UIButtonTypeCustom按钮可以设置title。
若置title于图像上面时,可使用setBackgroundImage;
若置title于图像右边时,可使用setImage,且要设置frame宽度大于图像,以能显示出title文字。
设置titleEdgeInsets可实现文字到图片下方,不过要经过一翻计算。
setImage的图的Z坐标是最高的。
4.光晕效果
button.showsTouchWhenHighlighted=YES;点击时的闪光效果会被前景图片遮住中间部分;
Shows Touch On Highlight (高亮)光晕的大小是55x55像素,大于40x40像素的按钮不能使用该视觉效果。
5.指定目标函数传递的参数问题
例如
[button addTarget:self action:@selector(tableView:accessoryButtonTappedForRowWithIndexPath:) forControlEvents:UIControlEventTouchUpInside];,
在执行时,传递给tableView函数的参数类型分别是UIButton类型和UITouchesEvent类型。即不论函数原型是什么,button实际传递的参数类型是固定的。
6.点击测试UIButton响应UIControlEventTouchUpInside事件时,响应点超出了它button的范围。
7.在UIButton中addSubview的问题
UIView的userInteractionEnabled值默认为YES,必须设置UIButton所有的subview的userInteractionEnabled为NO,才能让UIButton正常响应点击。
但是如果设置了UIView的setUserInteractionEnabled为NO,其子view都将得不到响应。
8.处理双击问题
[button addTarget:self action:@selector(onTouchUpInside:withEvent:) forControlEvents:UIControlEventTouchUpInside];
-(void)onTouchUpInside:(id)sender withEvent:(UIEvent*)event
{
UITouch* touch = [[event allTouches] anyObject];
NSLog(@"onTouchUpInside tagCount:%d",touch.tapCount);
//判断点击次数
if (touch.tapCount == 1)
{
//todo
}
}