使用UIButton

  1. //login button  
  2.     //  .h 中定义  
  3.     UIButton *_loginBtn;  
  4.     @property (strong,nonatomic)UIButton *loginBtn;  
  5.       
  6.       
  7.     // .m 中实现设置按钮  
  8.     @synthesize loginBtn = _loginBtn;//使用备份变量名  
  9.       
  10.     //设置按钮的  形状  
  11.     self.loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  12.     /* 
  13.      buttonWithType:  定义button按钮的外形 
  14.      六种定义button类型: 下面有图解 
  15.      UIButtonTypeCustom = 0,    无类型 
  16.      UIButtonTypeRoundedRect,    四个角是圆弧   型的 
  17.      UIButtonTypeDetailDisclosure, 
  18.      UIButtonTypeInfoLight, 
  19.      UIButtonTypeInfoDark, 
  20.      UIButtonTypeContactAdd, 
  21.      */  
  22.       
  23.     //定义button按钮在frame上的坐标(位置),和这个按钮的宽/高  
  24.     self.loginBtn.frame = CGRectMake(40, 200, 80, 30);  
  25.       
  26.       
  27.     [self.loginBtn setTitle:@"Login" forState:UIControlStateNormal];  
  28.     /* 
  29.      常用的属性: 
  30.       setTitle:  设置button按钮的名称 
  31.       setImage: [UIImage imageNamed:@"图名"]  添加图片 
  32.       setTitleColor:[UIColor redColor]  设置字体颜色 
  33.       
  34.      forState 设置 按钮点击前后的状态   : 下有图解 
  35.      UIControlStateHighlighted 
  36.      UIControlStateSelected 
  37.      UIControlStateDisabled 
  38.      UIControlStateNormal 
  39.       
  40.      */  
  41.       
  42.     // 为按钮添加一个动作  
  43.     //  action:  如果点击的话执行的方法  
  44.     [self.loginBtn addTarget:self action:@selector(Login:) forControlEvents:UIControlEventTouchUpInside];  
  45.       
  46.     //把button控件添加到view中显示  
  47.     [self.view addSubview:self.loginBtn];  

[cpp]  view plain copy
  1. //执行动作的方法  
  2. -(IBAction)Login:(id)sender;  


六种定义button类型: 

     UIButtonTypeCustom = 0,   无类型

     UIButtonTypeRoundedRect,   四个角是圆弧  型的   


     UIButtonTypeDetailDisclosure    

     UIButtonTypeInfoLight    


     UIButtonTypeInfoDark    


     UIButtonTypeContactAdd    





forState 设置 按钮点击前后的状态   

        点击前                                         点击后

UIControlStateHighlighted


UIControlStateSelected   


UIControlStateDisabled   


      UIControlStateNormal     



UIButtonTypeRoundedRect 设置为这个属性,是可以满足我们普通情况下的按钮圆角,当我们在button上添加背景图片和背景颜色的时候就会发现,这个属性并不适用,因为现在的button已经不是圆角的了,它显示的是图片的形状,当设置背景颜色设置为UIButtonTypeCustom属性才可以显示出来。所以我们需要用UIButton控件的其它属性来满足我们的需求

[cpp]  view plain copy
  1. UIButton *btn;  
  2.     [btn.layer setMasksToBounds:YES];  
  3.     [btn.layer setCornerRadius:10.0];//设置矩形四个圆角半径  
  4.       
  5.     /* 
  6.         [btn.layer setBorderWidth:1.0];//边框宽度 
  7.      */  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值