您的位置:首页 > 产品设计 > UI/UE

IOS学习笔记(三)之UIButton

2016-03-29 23:39 435 查看
龟速前行中,UIButton主要花时间学习了代码方式创建使用。

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

UIButtonType

typedef enum {  

    UIButtonTypeCustom = 0,           //    自定义,无风格  

    UIButtonTypeRoundedRect,   / 白色圆角矩形,类似偏好设置表格单元或者地址簿卡片  

    UIButtonTypeDetailDisclosure,//蓝色的披露按钮,可放在任何文字旁  

    UIButtonTypeInfoLight,//微件(widget)使用的小圆圈信息按钮,可以放在任何文字旁  

    UIButtonTypeInfoDark,//白色背景下使用的深色圆圈信息按钮  

    UIButtonTypeContactAdd,//蓝色加号(+)按钮,可以放在任何文字旁  

} 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=[self
createButtonFrame:CGRectMake(10,
190,
self.view.frame.size.width-20,
37)
backImageName:nil
title:@"登录"
titleColor:[UIColor
whiteColor] 
font:[UIFont
systemFontOfSize:19]
target:self
action:@selector(landClick)];

    landBtn.backgroundColor=[UIColor
colorWithRed:248/255.0f
green:144/255.0f
blue:34/255.0f
alpha: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=[UIButton
buttonWithType:UIButtonTypeCustom];
    btn.frame=frame; //设置button的在屏幕中的位置,大小
 

    if (imageName)

    {

        [btn setBackgroundImage:[UIImage
imageNamed:imageName] forState:UIControlStateNormal];

    }

    

    if (font)

    {

        btn.titleLabel.font=font;

    }

    

    if (title)

    {

        [btn setTitle:title
forState:UIControlStateNormal];

    }

    if (color)

    {

        [btn setTitleColor:color
forState:UIControlStateNormal];

    }

    if (target&&action)

    {

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

    }

    return btn;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: