2.3 UIButton

【UIButton的作用】

UIButton为按钮,继承自UIControl类。使用UIButton可以让用户和APP进行交互

【UIButton的创建】

1)initWithFrame:方法

    UIButton *btn1 = [[UIButtonalloc]initWithFrame:CGRectMake(60,60,200,60)];

    btn1.backgroundColor = [UIColorredColor];

    [btn1 setTitle:@"btn1"forState:UIControlStateNormal];

    [self.windowaddSubview:btn1];

2)buttonWithType:方法

    UIButton *btn2 = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    btn2.frame =CGRectMake(60,120,200,60);

    btn2.backgroundColor = [UIColoryellowColor];

    [btn2 setTitle:@"btn2"forState:UIControlStateNormal];

    [self.windowaddSubview:btn2];

【UIButton的类型】

1)UIButtonTypeRoundedRect - 圆角按钮

2)UIButtonTypeDetailDisclosure - 详情按钮

3)UIButtonTypeContactAdd - 加号按钮

4)UIButtonTypeInfoDark - 白色信息按钮

5)UIButtonTypeInfoLight - 灰色信息按钮

6)UIButtonTypeCustom - 自定义按钮

【UIButton的常用属性】

1)frame - 坐标

2)tag - 标签(用来给控件添加唯一标识)

3)adjustsImageWhenHighlighted - 高亮状态下按钮是否变暗

4)adjustsImageWhenDisabled - 禁用状态下按钮是否变暗

5)showsTouchWhen - 按钮高亮状态下是否发光

【UIButton的常用方法】

1)/设置按钮上的文字

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

    [btn1 setTitle:@"btn1"forState:UIControlStateNormal];

2)// 设置按钮上标题文字的颜色

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

    [btn1 setTitleColor:[UIColorblackColor]forState:UIControlStateNormal];

3)// 设置按钮上标题文字的阴影

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

    [btn1 setTitleShadowColor:[UIColorblueColor]forState:UIControlStateNormal];

    [btn1.titleLabelsetShadowOffset:CGSizeMake(0, -1)];

4)// 设置按钮上的图片(不会被拉伸)

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

    [btn1 setImage:[UIImageimageNamed:@"ios/p.png"]forState:UIControlStateNormal];

5)// 设置按钮背景图(会拉伸充满整个btn

-(void)setBackgroundImage:(UIImage *)imageforState:(UIControlState)state;

    [btn1 setBackgroundImage:[UIImageimageNamed:@"ios/p.png"]forState:UIControlStateNormal];

6)// 设置按钮高亮状态下的颜色

-(void)setTintColor:(UIColor *)color;

    [btn1 setTintColor:[UIColorwhiteColor]];

【UIButton的事件】

1)-(void)addTarget:(id)targetaction:(SEL)actionforControlEvents:(UIControlEvents)controlEvents

2)-(void)removeTarget:(id)targetaction:(SEL)actionforControlEvents:(UIControlEvents)controlEvents





















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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值