综合使用

2016-03-09  本文已影响22人  飞翔的道长

通过storyboard创建

通过代码创建button

//可以指定创建类型
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
等同于
UIButton *btn = [[UIButton alloc]init];
[btn addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];

 - (void)add
{
    // 添加图片
    UIImageView *iconView = [[UIImageView alloc] init];
    iconView.image = [UIImage imageNamed:@"danjianbao"];
    iconView.frame = CGRectMake(0, 0, 50, 50);
    [self.shopsView addSubview:iconView];
    
    // 添加文字
    UILabel *label = [[UILabel alloc] init];
    label.text = @"单肩包";
    label.frame = CGRectMake(0, 50, 50, 20);
    label.font = [UIFont systemFontOfSize:11];
    label.textAlignment = NSTextAlignmentCenter;
    [self.shopsView addSubview:label];
}
    //新建一个按钮
    UIButton *btnTest = [[UIButton alloc]init];
    btnTest.titleLabel.font = [UIFont systemFontOfSize:16];//title字体大小  
    btnTest.titleLabel.textAlignment = NSTextAlignmentCenter;//设置title的字体居中  
    //指定按钮的位置及宽高
    btnTest.frame = CGRectMake(30, 30, 50, 50);
    //设置按钮显示的文字为test
    [btnTest setTitle:@"test" forState:UIControlStateNormal];
    //设置按钮文字颜色为蓝色
    [btnTest setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    //给按钮添加点击事件
    [btnTest addTarget:self action:@selector(remove) forControlEvents:UIControlEventTouchUpInside];
上一篇下一篇

猜你喜欢

热点阅读