iOS系列UI篇——UIButton

不说废话,直接上代码,温故而知新,确实受益良多!其中的奥妙,自己体会!
#import "ViewController.h"
#define screen_width [UIScreen mainScreen].bounds.size.width
#define screen_height [UIScreen mainScreen].bounds.size.height
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createButton];
}
- (void)createButton
{
    float bw = 50;
    float bh = 50;
    //设置button的类型
    /*
     UIButtonTypeCustom 自定
     UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0) iOS7默认
     UIButtonTypeDetailDisclosure 感叹号
     UIButtonTypeInfoLight 感叹号
     UIButtonTypeInfoDark 感叹号
     UIButtonTypeContactAdd 加号
     */
    UIButton * b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    //设置frame值
    b.frame = CGRectMake((screen_width - bw) / 2, (screen_height - bh) / 2, bw, bh);
    //设置背景色
    b.backgroundColor = [UIColor redColor];
    //设置按钮标题
    [b setTitle:@"按钮" forState:(UIControlStateNormal)];
    //设置按钮标题颜色
    [b setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    //设置不同状态下文本阴影色
    [b setTitleShadowColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
    //添加背景图片
    [b setImage:[UIImage imageNamed:@"1.jpg"] forState:(UIControlStateNormal)];

    //为button添加点击事件
    [b addTarget:self action:@selector(buttonClicked:) forControlEvents:(UIControlEventTouchUpInside)];
    //为button添加tag值
    b.tag = 200;
    [self.view addSubview:b];
}
- (void)buttonClicked:(UIButton *)sender
{
    //设置背景图片
    [sender setBackgroundImage:[UIImage imageNamed:@"1.jpg"] forState:(UIControlStateNormal)];
    [sender setTitle:@"选中" forState:(UIControlStateSelected)];
    sender.selected = YES;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值