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