动态创建控件
//
// ViewController.m
// MediaIOS
//
// Created by Inke219223m on 2022/5/23.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
//动态控件在里面创建
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//创建按钮
//UIButton *ubOne = [[UIButton alloc] init];//默认
//按钮类型
//UIButton *ubOne = [UIButton buttonWithType:UIButtonTypeContactAdd];
UIButton *ubOne = [UIButton buttonWithType:UIButtonTypeCustom];
//设置按钮文字 默认状态
[ubOne setTitle:@"点击我吧" forState:UIControlStateNormal];
//高亮
[ubOne setTitle:@"后端码匠" forState:UIControlStateHighlighted];
UIImage *uimg = [UIImage imageNamed:@"汉堡"];
UIImage *uimg2 = [UIImage imageNamed:@"薯条"];
//设置默认状态图片
[ubOne setBackgroundImage:uimg forState:UIControlStateNormal];
//设置高亮
[ubOne setBackgroundImage:uimg2 forState:UIControlStateHighlighted];
//设置默认状态下文字yanse
[ubOne setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[ubOne setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
//设置按钮的frame 方可显示
ubOne.frame = CGRectMake(50, 100, 100, 100);
//设置点击事件
[ubOne addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
//把动态创建的按钮添加进
[self.view addSubview:ubOne];
}
-(void) buttonClick {
NSLog(@"点击了,单击事件");
}
@end