UIButton 基本用法

UILabel 基本用法

总代码

写在viewDidLoad中

	//按 圆角类型 创建button
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect] ;
    
    //确定位置
    btn.frame = CGRectMake(100, 300, 160, 40) ;
    
    //button背景色
    btn.backgroundColor = [UIColor grayColor] ;
    
    //主屏幕颜色
    self.view.backgroundColor = [UIColor redColor] ;
    
    //按钮未按下状态 字
    [btn setTitle:@"我的" forState:UIControlStateNormal] ;
    
    //按钮按下状态 字
    [btn setTitle:@"你的" forState:UIControlStateHighlighted] ;
    
    //按钮未按下状态 颜色 UIControlStateNormal
    [btn setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal] ;
    
    //按钮按下状态 颜色 UIControlStateHighlighted
    [btn setTitleColor:[UIColor brownColor] forState:UIControlStateHighlighted] ;
    
    //按钮一直的颜色
    //[btn setTintColor:[UIColor grayColor]] ;
    
    //按钮 字 字号
    btn.titleLabel.font = [UIFont systemFontOfSize:24] ;

    //显示按钮
    [self.view addSubview:btn] ;

效果图如下:
未点击:
未点击
点击时:
点击时

注意:button不可以用btn.titleLabel.text来编辑文字

button类型

如下:button类型
以下是每个按钮的效果:
按钮效果

UIButton进阶

给按钮加点击事件

接上面的代码

    //按下按钮 后 跳转到 pressof
    //UIControlEventTouchUpInside    在按钮范围内松开
    //UIControlEventTouchUpOutside   在按钮范围外松开
    //UIControlEventTouchDown        点下按钮
    [btn addTarget:self action:@selector(pressof:) forControlEvents:UIControlEventTouchUpInside] ;

再新建一个点击事件

- (void)pressof:(UIButton*) btn
{
     NSLog(@"按下111") ;
}

给按钮做标记

    //标记为 101
    btn.tag = 101 ;

这样在点击事件中可以判断点击的是哪个按钮,从而作出不同的效果

- (void)pressof:(UIButton*) btn
{
    if (btn.tag == 101) {
        NSLog(@"按下111") ;
    }
    if (btn.tag == 102) {
        NSLog(@"按下222") ;
    }
}

设置图片按钮

//创建一个自定义类型按钮 btnImage
    UIButton* btnImage = [UIButton buttonWithType:UIButtonTypeCustom] ;

    //确定位置
    btnImage.frame = CGRectMake(100, 100, 200, 200) ;

    //引用图片 定义为 UIImage 类型
    UIImage* ima1 = [UIImage imageNamed:@"1.jpg"] ;
    UIImage* ima2 = [UIImage imageNamed:@"2.jpg"] ;

    //定义 btnImage 未按下时图片
    [btnImage setImage:ima1 forState:UIControlStateNormal] ;

    //定义 btnImage 按下时图片
    [btnImage setImage:ima2 forState:UIControlStateHighlighted] ;
    
    //显示按钮
    [self.view addSubview:btnImage] ;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值