UISB Button

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

// 创建普通按钮
-(void) createUIRectButton
{
    //创建一个btn对象,根据类型创建btn,
    //圆类型btn: UIButtonTypeRoundedRect
    //通过类方法来创建buttonWithType : 类名+ 方法名
    //不会通过 alloc 创建内存
    UIButton* btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    btn.frame=CGRectMake(100, 100, 100, 40);
    //按钮背景颜色
    btn.backgroundColor=[UIColor greenColor];
    //设置文字显示的颜色
    //P1 颜色
    //P2 状态
//    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    
    
    
    // 设置按钮上的文字
    //@parameter
    //P1 : 字符串类型 ,显示在按钮上的文字
    //P2 : 设置文字显示状态类型 :UIControlStateNormal ,正常状态
    [btn setTitle:@"按钮01" forState:UIControlStateNormal];
    //P1 显示文字
    //P2 : 设置文字显示状态类型 :UIControlStateHighlighted ,按下状态
    [btn setTitle:@"按钮按下" forState:UIControlStateHighlighted];
    
    //设置按钮风格颜色
    [btn setTintColor:[UIColor yellowColor]];
    
    
    //按钮颜色大小
    btn.titleLabel.font=[UIFont systemFontOfSize:20];
    
    
    [self.view addSubview:btn];
    
}


-(void) createImageBtn
{
    //创建一个自定义btn
    UIButton *btnImage=[UIButton buttonWithType:UIButtonTypeCustom];
    btnImage.frame=CGRectMake(100, 100, 100, 40);
    btnImage.backgroundColor=[UIColor greenColor];
    UIImage* icon01=[UIImage imageNamed:@"taobao"];
    UIImage* icon02=[UIImage imageNamed:@"tianmao"];
    
    //设置按钮图片方法设置
    //P1 显示的图片对象
    //P2 控件的状态
    
    
    [btnImage setImage:icon01 forState:UIControlStateNormal];
    [btnImage setImage:icon02 forState:UIControlStateHighlighted];
    
    [self.view addSubview:btnImage];
    
    
    
    
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [self createUIRectButton];
    
    [self createImageBtn];
}


@end
原文地址:https://www.cnblogs.com/zhangqing979797/p/13629518.html