IOS纯代码开发UIButton按钮

IOS纯代码开发UIButton按钮

//
//  ViewController.m
//  04动态创建按钮
//
//  Created by 鲁军 on 2021/1/30.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //UIButton *button = [[UIButton alloc] init];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button setTitle:@"点我吧" forState:UIControlStateNormal];
    [button setTitle:@"摸我干啥" forState:UIControlStateHighlighted];
    
    //设置不同状态下的颜色
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    
    
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
    
    UIImage *imgNormal = [UIImage imageNamed:@"btn_01"];
    
    UIImage *imgHighlighted = [UIImage imageNamed:@"btn_02"];
    
    [button setBackgroundImage:imgNormal forState:UIControlStateNormal];
    [button setBackgroundImage:imgHighlighted  forState:UIControlStateHighlighted];
    
    
    button.frame = CGRectMake(50, 100,100, 100);
    
    
    //单击事件
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    //动态的创建的按钮加到控制器的管理的那个view里面
    
    [self.view addSubview:button];
    
    
    
    
}

-(void)buttonClick{
    
    NSLog(@"1234");
}


@end

上一篇:iOS聊天起泡(背景图片被拉伸不变形)----转载--待验证


下一篇:Linux-Windows10双系统安装