IOS学习笔记(三)之UIButton

来源:互联网 发布:sql 数据库 培训 编辑:程序博客网 时间:2024/05/23 17:56

龟速前行中,UIButton主要花时间学习了代码方式创建使用。


UIButton创建方式分为两种:intWithFrame、buttonWithType


UIButtonType

  1. typedef enum {  
  2.     UIButtonTypeCustom = 0,           //    自定义,无风格  
  3.     UIButtonTypeRoundedRect,   / 白色圆角矩形,类似偏好设置表格单元或者地址簿卡片  
  4.     UIButtonTypeDetailDisclosure,//蓝色的披露按钮,可放在任何文字旁  
  5.     UIButtonTypeInfoLight,//微件(widget)使用的小圆圈信息按钮,可以放在任何文字旁  
  6.     UIButtonTypeInfoDark,//白色背景下使用的深色圆圈信息按钮  
  7.     UIButtonTypeContactAdd,//蓝色加号(+)按钮,可以放在任何文字旁  
  8. } UIButtonType; 

UIButton常用属性

//设置对应状态的标题内容default is nil. title is assumed to be single line

- (void)setTitle:(NSString *)title forState:(UIControlState)state;  

//设置对应状态的标题颜色           

- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;   

//设置对应状态的标题阴影颜色            

- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;          

//设置对应状态的按钮的图片

- (void)setImage:(UIImage *)image forState:(UIControlState)state;        

//设置对应状态的按钮背景图片

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;


UIButton添加事件:

[btn addTarget:<#(id)#> action:<#(SEL)#> forControlEvents:<#(UIControlEvents)#>] 


例如:

UIButton *landBtn=[selfcreateButtonFrame:CGRectMake(10,190, self.view.frame.size.width-20,37) backImageName:niltitle:@"登录"titleColor:[UIColorwhiteColorfont:[UIFontsystemFontOfSize:19]target:selfaction:@selector(landClick)];

    landBtn.backgroundColor=[UIColorcolorWithRed:248/255.0fgreen:144/255.0fblue:34/255.0falpha:1];

    landBtn.layer.cornerRadius=5.0f;


-(UIButton *)createButtonFrame:(CGRect)frame backImageName:(NSString *)imageName title:(NSString *)title titleColor:(UIColor *)color font:(UIFont *)font target:(id)target action:(SEL)action

{

    UIButton *btn=[UIButtonbuttonWithType:UIButtonTypeCustom];

    btn.frame=frame; //设置button的在屏幕中的位置,大小 

    if (imageName)

    {

        [btn setBackgroundImage:[UIImageimageNamed:imageName] forState:UIControlStateNormal];

    }

    

    if (font)

    {

        btn.titleLabel.font=font;

    }

    

    if (title)

    {

        [btn setTitle:titleforState:UIControlStateNormal];

    }

    if (color)

    {

        [btn setTitleColor:colorforState:UIControlStateNormal];

    }

    if (target&&action)

    {

        [btn addTarget:targetaction:action forControlEvents:UIControlEventTouchUpInside];

    }

    return btn;

}






0 0
原创粉丝点击