UI-最基本的UIButton的创建

// 1.1创建按钮对象

// 1.2设置按钮的类型

// 注意:设置按钮的类型只能在初始化的时候设置--> UIButtonTypeCustom

UIButton*button1 = [UIButton buttonWithType:UIButtonTypeCustom];

// 1.3设置frame

button1.frame=CGRectMake(100,100,170,60);

// 1.4设置背景颜色

button1.backgroundColor= [UIColor redColor];

// 1.5设置文字

[button1setTitle:@"我们在一起吧!"forState:UIControlStateNormal];

[button1setTitle:@"我们"forState:UIControlStateHighlighted];

// 1.6设置文字颜色

[button1setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];

[button1setTitleColor:[UIColor yellowColor]forState:UIControlStateHighlighted];

// 1.7设置文字的阴影颜色

[button1setTitleShadowColor:[UIColor blackColor]forState:UIControlStateNormal];

[button1setTitleShadowColor:[UIColor greenColor]forState:UIControlStateHighlighted];

button1.titleLabel.shadowOffset=CGSizeMake(3,2);

// 1.8设置内容图片

[button1setImage:[UIImage imageNamed:@"tabbar_rank"]forState:UIControlStateNormal];

[button1setImage:[UIImage imageNamed:@"tabbar_rank"]forState:UIControlStateHighlighted];

button1.imageView.backgroundColor= [UIColor purpleColor];

// 2.0加到控制器的view中

[self.view addSubview:button1];

/**

*非常重要

*/

[button1addTarget:selfaction:@selector(clickButton:)forControlEvents:UIControlEventTouchUpInside];

}

// 此处关联的是button的方法

- (void)clickButton:(UIButton*)sender

{

NSLog(@"%@",sender);

}

你可能感兴趣的:(UI-最基本的UIButton的创建)