IOS成长之路-UIButton定义和设置圆角

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

[cpp] view plaincopy
  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 plaincopy
  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