UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(60, 60, 200, 60)]; btn.backgroundColor=[UIColor greenColor]; [btn setTitle:@"btn1" forState:UIControlStateNormal]; [self.view addSubview:btn];
UIButton *btn2=[UIButton buttonWithType:UIButtonTypeRoundedRect]; CGRect rect=CGRectMake(60, 160, 200, 60); btn2.frame=rect; btn2.tag=1001; btn2.backgroundColor=[UIColor colorWithRed:30/255.0 green:200/255.0 blue:125/255.0 alpha:1.0]; [btn2 setTitle:@"btn2" forState:UIControlStateNormal]; [btn2 addTarget:self action:@selector(btn2Pressed) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:btn2];
Btn2Pressed method:
-(void)btn2Pressed{ NSLog(@"button pressed");}
Typedef enum {UIButtonTypeCustom = 0, // no button type custom, no style UIButtonTypeRoundedRect, // rounded rect, flat white button, like in address card white rounded rectangle, similar to preference settings, the table unit or address book card UIButtonTypeDetailDisclosure, // The Blue disclosure button can be placed next to any text, and the tiny circle information button used by the UIButtonTypeInfoLight, // widget, you can put UIButtonTypeInfoDark next to any text, // The UIButtonTypeContactAdd button for the dark circle information used in the white background, // The Blue plus sign (+) button, which can be placed next to any text} UIButtonType;
Void) setTitle :( NSString *) title forState :( UIControlState) state;
Void) setTitleColor :( UIColor *) color forState :( UIControlState) state;
Void) setTitleShadowColor :( UIColor *) color forState :( UIControlState) state;
Void) setImage :( UIImage *) image forState :( UIControlState) state;
Void) setBackgroundImage :( UIImage *) image forState :( UIControlState) state;
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};
[btn addTarget:<#(id)#> action:<#(SEL)#> forControlEvents:<#(UIControlEvents)#>]
typedef NS_OPTIONS(NSUInteger, UIControlEvents) { UIControlEventTouchDown = 1 << 0, // on all touch downs UIControlEventTouchDownRepeat = 1 << 1, // on multiple touchdowns (tap count > 1) UIControlEventTouchDragInside = 1 << 2, UIControlEventTouchDragOutside = 1 << 3, UIControlEventTouchDragEnter = 1 << 4, UIControlEventTouchDragExit = 1 << 5, UIControlEventTouchUpInside = 1 << 6, UIControlEventTouchUpOutside = 1 << 7, UIControlEventTouchCancel = 1 << 8, UIControlEventValueChanged = 1 << 12, // sliders, etc. UIControlEventEditingDidBegin = 1 << 16, // UITextField UIControlEventEditingChanged = 1 << 17, UIControlEventEditingDidEnd = 1 << 18, UIControlEventEditingDidEndOnExit = 1 << 19, // 'return key' ending editing UIControlEventAllTouchEvents = 0x00000FFF, // for touch events UIControlEventAllEditingEvents = 0x000F0000, // for UITextField UIControlEventApplicationReserved = 0x0F000000, // range available for application use UIControlEventSystemReserved = 0xF0000000, // range reserved for internal framework use UIControlEventAllEvents = 0xFFFFFFFF};
@ Zhang xingye TBOW
Http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIButton_Class/UIButton/UIButton.html
Http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKit_Framework/Introduction/Introduction.html#//apple_ref/doc/uid/TP40006955-CH1-SW1