iOS button总结

  1.  //    设置按钮类型,此处为圆角按钮  
  2.     UIButton *writeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  3.     //    设置和大小  
  4.     CGRect frame = CGRectMake(100.0f, 100.0f, 140.0f, 50.0f);  
  5.     //    将frame的位置大小复制给Button  
  6.     writeButton.frame = frame;  
  7.       
  8. //-----------------------------------------------  
  9.     //  给Button添加标题    
  10.         [writeButton setTitle:@"代码按钮" forState:UIControlStateNormal];  
  11.     //   设置按钮背景颜色   
  12.         writeButton.backgroundColor = [UIColor clearColor];  
  13.     //  设置按钮标题文字对齐方式,此处为左对齐  
  14.     writeButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;  
  15.     //使文字距离做边框保持10个像素的距离。  
  16.       writeButton.contentEdgeInsets = UIEdgeInsetsMake(0,30, 0, 0);  
  17. //----------------------------------------------------  
  18.       
  19. /******************************************************    
  20.  //此处类容目的掩饰代码代码操作按钮一些属性,如果设置按钮背景为图片可以将此处注释取消,注释掉上没横线范围类代码,进行测试 
  21.   
  22.     //    设置按钮背景图片 
  23.     UIImage *image= [UIImage imageNamed:@"background.png"]; 
  24.      
  25.     [writeButton setBackgroundImage:image forState:UIControlStateNormal]; 
  26.     //  按钮的相应事件   
  27.   
  28. *****************************************************/  
  29.     [writeButton addTarget:self action:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];  
  30.       
  31.     [self.view addSubview:writeButton];  
  32. }  

  1. <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>  
  2.    
  3.    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>  
  4.    
  5.    UIButtonTypeRoundedRect,     <span style="color:rgb(17,116,10)"><em>// 圆角风格按钮</em></span>  
  6.    
  7.    UIButtonTypeDetailDisclosure,    <span style="color:rgb(17,116,10)"><em>// </em></span>  
  8.    
  9.    UIButtonTypeInfoLight,       <span style="color:rgb(17,116,10)"><em>// 明亮背景的信息按钮</em></span>  
  10.    
  11.    UIButtonTypeInfoDark,        <span style="color:rgb(17,116,10)"><em>// 黑暗背景的信息按钮</em></span>  
  12.    
  13.    UIButtonTypeContactAdd,      <span style="color:rgb(17,116,10)"><em>//</em></span>  
  14.    
  15. <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
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值