UIButton的常用方法
//初始化时设置Button样式,个人喜欢用圆角样式
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//UIButton的几种样式
//typedef NS_ENUM(NSInteger, UIButtonType) {
// UIButtonTypeCustom = 0, // //no button type
// UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), // //standard system button
// UIButtonTypeDetailDisclosure,
// UIButtonTypeInfoLight,
// UIButtonTypeInfoDark,
// UIButtonTypeContactAdd,
// UIButtonTypeRoundedRect = UIButtonTypeSystem, // //Deprecated, use UIButtonTypeSystem instead
//};
//设置button的位置和大小
button.frame = CGRectMake(50, 50, 100, 30);
//设置button背景色
button.backgroundColor = [UIColor magentaColor];
//设置在什么状态下显示什么文字
[button setTitle:@"按钮" forState:UIControlStateNormal];
//设置在什么状态下字体什么颜色
[button setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];
//设置背景图片
[button setBackgroundImage:[UIImage imageNamed:@"image.jpg"] forState:UIControlStateNormal];
//UIControlState的样式
// typedef NS_OPTIONS(NSUInteger, UIControlState) {
// UIControlStateNormal = 0,
// UIControlStateHighlighted = 1 << 0, // used when UIControl isHighlighted is set
// UIControlStateDisabled = 1 << 1,
// UIControlStateSelected = 1 << 2, // flag usable by app (see below)
// UIControlStateApplication = 0x00FF0000, // additional flags available for application use
// UIControlStateReserved = 0xFF000000 // flags reserved for internal framework use
// };
//给button添加响应事件
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//添加到view
[self.view addSubview:button];
//button的响应事件
- (void)buttonAction:(UIButton*)sender {
NSLog(@"Hello world");
}