- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//创建一个按钮
UIButton *button = [[UIButton alloc]init];
//添加到根视图UIView
[self.view addSubview:button];
/*
button在这里是局部变量,会担心{}之后,就被销毁,
但是addSubview可以将button与self.view之间加上强指针,
只要self.view没有销毁,button就不会销毁
*/
//设置frame
button.frame = CGRectMake(112, 100, 150, 150);
//设置文字
[button setTitle:@"点我啊" forState:UIControlStateNormal];
[button setTitle:@"哇哇~" forState:UIControlStateHighlighted];
//创建图片
UIImage *image1 =[UIImage imageNamed:@"button1"];
UIImage *image2 =[UIImage imageNamed:@"button2"];
//设置背景图
[button setBackgroundImage:image1 forState:UIControlStateNormal];
[button setBackgroundImage:image2 forState:UIControlStateHighlighted];
}
|
|